Plot date and time

1 view (last 30 days)
Trader
Trader on 29 Mar 2012
Edited: KRUNAL on 12 Aug 2014
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
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
Trader on 3 Apr 2012
Geoff - Could you give me your suggestion on how to use cellfun to accomplish this? I'm interested in learning.
KRUNAL
KRUNAL on 12 Aug 2014
Edited: KRUNAL on 12 Aug 2014
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 ?

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!