How to plot a graph that have both dates and time in the same x-axis
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Now I got an excel file which contains two columns,which the first column is the date (DD/MM/YYYY)and the second column is the time ( HH:MM:SS). Below is my program code
z=outCell(1:length(xlsread(FileNames(i).name)),11);
y= outCell(1:length(xlsread(FileNames(i).name)),1);
x=outCell(1:length(xlsread(FileNames(i).name)),2);
c=cell2mat(z);
c1=c-7.727;
s=char(y);
t=char(x);
d1=datenum(s,'D/M/yyyy');
d2=datenum(t,'HH:MM:SS');
d=d1+d2;
plot(d,c1);
end
hold on
datetick(d,'dd,mm,yy,HH,MM,SS');
------------------------------------------ Many thanks, Alex
0 comentarios
Respuestas (1)
dpb
el 14 de Ag. de 2014
...
plot(d,c1);
datetick('x','dd/mm/yy HH:MM:SS','keeplimits');
See
doc datetick
for details.
You'll likely need to modify the date format string or cut down the number of ticks significantly or will quickly run out of real estate to display the date string tick values.
2 comentarios
dpb
el 14 de Ag. de 2014
Of course Matlab "accepts" d1+d2. What does "cannot plot" mean, specifically?
Have you verified you've gotten a valid time conversion? What does
datestr(d)
return? Is it the correct date/time?
Oh, I just noticed--you've got an inconsistent date format string in the conversion line as compared to what your description of the date format in the file is--
...the first column is the date (DD/MM/YYYY)...
d1=datenum(s,'D/M/yyyy');
The 'D/M' portion doesn't match the integer portion--day and month abbreviations are lower-case and the single letter indicates using the first letter of the day/month. The 2-digit form is 'dd/mm' so your date conversion string should be
d1=datenum(s,'dd/mm/yyyy');
This should then work ok altho it would be more efficient to concatenate the date and time strings and use the full conversion at one time --
dn=datenum([x y],'dd/mm/yyyy HH:MM:SS');
Then you can dispense with the d1+d2 and just plot vs dn
See
doc datestr % for the conversion definitions
Also, it's terribly inefficient to read the file three times to get the data in the beginning --
[numbs,text]=xlsread(FileNames(i).name); % read the file
dn=datenum([text(:,1) text(:,2)],'mm/dd/yyyy HH:MM:SS');
Similarly for the numeric data which will be in the numbs array. You'll have to fixup column numbers correctly, of course.
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!