How to plot a graph that have both dates and time in the same x-axis

1 次查看(过去 30 天)
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

回答(1 个)

dpb
dpb 2014-8-14
...
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 个评论
NG
NG 2014-8-14
Thanks a lot ! I can now datetick the time and date now. However, matlab cannot plot (d,c1), where d=d1+d2,i think matlab does not accept the code for d= d1+d2 , is there any way to fix this problem?
dpb
dpb 2014-8-14
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by