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

7 vues (au cours des 30 derniers jours)
Sina Shojaei
Sina Shojaei le 29 Août 2015
Commenté : Star Strider le 6 Sep 2015
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.

Réponse acceptée

Star Strider
Star Strider le 29 Août 2015
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 commentaires
Sina Shojaei
Sina Shojaei le 6 Sep 2015
Thanks.silly me ;)
Star Strider
Star Strider le 6 Sep 2015
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.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 29 Août 2015
Modifié(e) : dpb le 29 Août 2015
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...

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by