Time axis based on the last 24 hours

Hello everyone!
I am trying to find a way to make my axis go from a certain time to the same time 24h later. What I mean is that I am taking measurements for the last 24 hours and I want my axis to be limited and have certain ticks.
More specifically, I get measurements which start at 10am on the 1st of Nov and it finishes at 10am on the 2nd of Nov. And i want my axis to go from 10 to 24 / 00 to 10 with 1h steps (ticks).
Any suggestions on how to do that are more than welcome.
Thanks a lot for your time.

1 commentaire

plot(timefin(l-288:l),y(l-288:l,26),'r','Linewidth', 2)
set(gca,'xtick',[timefin(l-288):(3/24):timefin(l)])
([timefin(l-288) timefin(l)])
set(gca,'xticklabel',{hour(l-288) hour(l-252) hour(l-216) hour(l-180) hour(l-144) hour(l-108) hour(l-72) hour(l-36) hour(l)})
Problem solved! Thanks for your help!

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 26 Oct 2016
If you have R2014b or later:
n = datetime()-hours(24);
x = n + hours(0:23);
plot(x, rand(1,24))
ax = gca; ax.XRuler.TickLabelFormat = 'HH'
(XRuler might not have been accessible until R2015a or R2015b)

2 commentaires

asdada
asdada le 26 Oct 2016
Hi and thanks for your response. I forgot to mention that I get the time in this format '2016-05-21 04:05:00' '2016-05-21 04:10:00' '2016-05-21 04:15:00' and I didn't even plot against the time. All I need is the axis to be in that format.
So I am essentially plotting 1 point every 5 mins, but I want the axis and the ticks to be labeled differently.
I am currently using datenum, but that gives me totally different numbers on the axis.
plot(timefin(l-288:l),y(l-288:l,26),'r','Linewidth', 2)
set(gca,'xtick',[timefin(l-288):(3/24):timefin(l)])
([timefin(l-288) timefin(l)])
set(gca,'xticklabel',{hour(l-288) hour(l-252) hour(l-216) hour(l-180) hour(l-144) hour(l-108) hour(l-72) hour(l-36) hour(l)})
Problem solved! Thanks for your help!

Connectez-vous pour commenter.

Question posée :

le 26 Oct 2016

Commenté :

le 26 Oct 2016

Community Treasure Hunt

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

Start Hunting!

Translated by