Ploting vs. 24hour day time (string that resets to zero and repeats)

1 次查看(过去 30 天)
I want to plot some data that looks like this( i.e, plot(time,x).
time= [ 12 14 16 18 20 22 0 2 4 6 8 10 12 14 16 18 20 22 0 2 4 6 8 10]
X= [ 30 32 34 30 29 26 22 20 18 19 20 28 30 32 34 30 29 26 22 20 18 19 20 28]
but looking for best way to keep order of the data as is. Currently I split the data between 0 times and plot on adjacent subplots which looks ugly. Any suggestions appreciated.

采纳的回答

Star Strider
Star Strider 2015-8-29
I suspect this may be what you want to do:
time = [ 12 14 16 18 20 22 0 2 4 6 8 10 12 14 16 18 20 22 0 2 4 6 8 10];
X = [ 30 32 34 30 29 26 22 20 18 19 20 28 30 32 34 30 29 26 22 20 18 19 20 28];
xt = 1:length(time); % X-Ticks
figure(1)
plot(xt, X)
grid
set(gca, 'XTick', xt, 'XTickLabels', time) % Label X-Axis With ‘time’ Vector
Apologise for the delay — hardware crash on my primary MATLAB computer a couple hour ago, so had to install new to this one.
  3 个评论
Star Strider
Star Strider 2015-9-6
My pleasure!
Not silly — it may not be something you would have likely encountered unless you read the documentation on ‘handle graphics’ in some detail.

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2015-8-29
编辑:dpb 2015-8-29
A) Convert times to a datenumber and plot against it instead. Use datetick to format the axis or use the new time class with builtin support in latest release(s), or
B) Plot versus index instead of actual time and set tick mark labels manually -- not particularly elegant so A) is the better option.
dn=datenum(2015,1,1,12+[0:2:2*length(time)-1].',0,0);
plot(dn,X)
xlim([dn(1) dn(end)]), ylim([15 35])
datetick('x','dd-HH','keeplimits')
Above uses arbitrary start date; it's immaterial to simply show times but datenum needs six values. Note the generation of the two-hour increments from the starting point to match the length of the data vector and that plotting is against the date numbers that are monotonic which solves your problem of "what to plot against?".
As noted, there's a new time class but I don't have latest release so can't demonstrate 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