Plot date and time
Show older comments
I have date and time values that I'd like to use for my XTick values. The data is in an array that has date, time and price. I would like to plot my price across multiple days and each time interval, 5 minute intervals. How can I set this up? Is it possible to set XTick to every 1.5 hours or every 18 ticks?
This is what I have right now but it doesn't accomplish what I want:
the array is 415x3 set like [date time price]
date = datenum(date,'mm/dd/yy');
time = datenum(time,'HH:MM');
count = length(time);
plot(price, count)
Thanks for your help!
Answers (1)
Thomas
on 29 Mar 2012
If you have date and time as cell's in Vectors date in format cell{mm/dd/yy} time in format cell{HH:MM:SS}
1) concatenate them
dt_time=[date time];
2) convert cell to mat
dt_time_new=cell2mat(dt_time);
3) convert to datenum here
final_time=datenum(dt_time_new,'mm/dd/yyHH:MM:SS');
plot(final_time,price)
datetick('x','mm/dd','keepticks') % or which ever format you want
7 Comments
Trader
on 29 Mar 2012
Thomas
on 29 Mar 2012
Can you show an example of date time array
Trader
on 30 Mar 2012
Geoff
on 30 Mar 2012
That line f.time will not give you a sensible result. I tried this. You need to do:
f.time = mod(datenum(itime,'HH:MM'), 1);
Then you can use:
final_time = f.date + f.time;
Alternatively, you can do this (if you don't need to split date and time in the first place):
f.datetime = datenum( [idate ' ' itime], 'mm/dd/yy HH:MM');
Geoff
on 30 Mar 2012
Oh, actually you probably can't do my last suggestion cos your strings are gonna be in a cell array. You could use cellfun, but given that you are asking how to apply Thomas' answer to your code which is nearly identical, maybe you should stay away from cellfun for now! =)
Trader
on 3 Apr 2012
Thomas I tried using it. It says datenum failed for line 23
This is my code.
clc;
clear all;
orginf = '<filename>';
sheet = 'Sheet1';
for n =1:151
xlrange = ['C' num2str(n+1)];
a = xlsread (orginf,sheet,xlrange);
if a==1
range = ['E' num2str(n+1)];
range1 = ['F' num2str(n+1)];
range2 = ['S' num2str(n+1)];
[~,~,b] = xlsread(orginf,sheet,range);
c = xlsread(orginf,sheet,range1);
d = {datestr(c,'HH:MM:SS')};
dt_time = [b,d];
[~,~,mag] = xlsread(orginf,sheet,range2);
mag_new = double(cell2mat(mag));
dt_time_new=cell2mat(dt_time)
final_time=datenum(dt_time_new,'mm/dd/yyHH:MM:SS'); %line 23
plot(final_time,mag_new)
datetick('x','mm/dd','keepticks')
end
end
and my data in the various columns are as follows :
Stage Magnitude Date Time
1 -1 06/10/2012 17:56:50
1 -3 06/10/2012 17:59:33
2 -2 06/11/2012 8:52:45
2 -5 06/11/2012 8:52:46
3 -3 06/12/2012 9:11:37
3 -4 06/12/2012 9:55:56
3 -1 06/12/2013 9:57:46
4 -5 06/12/2013 10:47:49
4 -2 06/12/2013 10:48:08
4 -4 06/12/2013 10:50:35
5 -3 06/12/2013 7:47:43
5 -1 06/12/2013 8:15:30
5 -2 06/12/2013 8:16:04
I am trying to plot magnitude v/s date time. Please suggest me what is wrong with it ?
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!