Show time in x axis with increments of 10minutes

조회 수: 24 (최근 30일)
Mathan
Mathan 2022년 4월 4일
댓글: Mathan 2022년 4월 5일
Hello,
I was wondering whether it is possible to plot a graph with time on x axis that varies with 10 minute (or 15 minute or 20 minute) increment (i.e. as per our choice). For instance, I have two times:
t_start = 10-Jun-2015 03:30:00 (which is the start time)
t_end = 10-Jun-2015 05:00:00 (is the end time)
How can I have a plot with x axis containing ticks such as 03:30, 03:40, 03:50........04:40, 04:50, 05:00.
I tried:
xtks = linspace(t_start, t_end, 20);
xticks(xtks);
xlim([t_start, t_end]);
datetick('x','HH:MM','keepticks', 'keeplimits');
But this just produces an x axis with 20 equally divided ticks (not increments as shown above). I tried with 10, 15, etc instead of 20 for xtks but still it does'not come out as expected.
Any help is appreciated.
Thanks
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 4월 4일
Is your t_start and t_end in the form of serial date numbers or in the form of datetime() objects? datetick() is only used for serial date numbers.
Mathan
Mathan 2022년 4월 4일
Both of them simply look like this (has this exact format and not decimal format):
t_start = '10-Jun-2015 03:30:00'
t_end = '10-Jun-2015 05:00:00'

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 4월 4일
t_start = datetime('10-Jun-2015 03:30:00'); %(which is the start time)
t_end = datetime('10-Jun-2015 05:00:00'); %(is the end time)
t = t_start : minutes(1) : t_end;
ndata = length(t);
data = rand(1, ndata);
plot(t, data);
xticks(t_start:minutes(15):t_end);
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 4월 4일
For serial date numbers,
incr = increment_in_minutes/(24*60)
xticks(t_start:incr:t_end)
and then datetick() like you show
Ideally in both cases more work should be done to round t_start to the start of the desired period, so you do not have the risk of ticks at (say) 3:17, 3:27, 3:37 and so on if the data happened to end like that. dateshift() can help for that.
Mathan
Mathan 2022년 4월 5일
Thank you again.

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

추가 답변 (0개)

카테고리

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