Show time in x axis with increments of 10minutes

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

请先登录,再进行评论。

更多回答(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