Plot date and time

조회 수: 3 (최근 30일)
Trader
Trader 2012년 3월 29일
편집: KRUNAL 2014년 8월 12일
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!

답변 (1개)

Thomas
Thomas 2012년 3월 29일
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
Trader
Trader 2012년 4월 3일
Geoff - Could you give me your suggestion on how to use cellfun to accomplish this? I'm interested in learning.
KRUNAL
KRUNAL 2014년 8월 12일
편집: KRUNAL 2014년 8월 12일
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 ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by