Plotting x-axis time in dd hh mm ss format
111 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Calum
el 4 de Abr. de 2023
Editada: Bora Eryilmaz
el 11 de Abr. de 2023
Hi folks, hope you are well.
I am just wondering if anyone has any experience with gps-type time scales for plotting. I am plotting a series of interactive plots and already have my x parameter (time in seconds) and my y parameters to match. However, I also have a matching time dataset which is in dd hh mm ss format, for day, hour, minute, second - where day started on the 1st january this year (locally).
I would really like to explore the possibility of having my x-axis time scale in the form dd hh mm ss as is given in the data, whilst STILL having the iteractivity component of these plots. i.e. being able to zoom and scale with the time snapping to ajust.
I have looked through plenty answers but can't find much help with what exactly i'm trying to do - hope someone can help!
Many thanks,
C.
0 comentarios
Respuesta aceptada
Bora Eryilmaz
el 4 de Abr. de 2023
Editada: Bora Eryilmaz
el 11 de Abr. de 2023
You can use the datetime data type for the x-axis values and adjust the date/time format as you like:
now = datetime;
x = now + seconds(0:10)/7;
y = rand(1,11);
plot(x,y)
ax = gca;
ax.XAxis.TickLabelFormat = 'HH:mm:ss.SSS';
ax.XTickLabelRotation = 45;
t = 4.3860e+04 + (0:100)/pi; % An array of seconds starting at 4.3860e+04 seconds.
ts = seconds(t); % Convert it to duration in seconds.
ts.Format = 'hh:mm:ss.SSS';
y = rand(size(t));
plot(ts,y)
ax = gca;
ax.XTickLabelRotation = 45;
All the zoom, pan, etc, behaviour would continue to be functional.
5 comentarios
Más respuestas (1)
Steven Lord
el 5 de Abr. de 2023
Based on the clarifications you posted on the answer by @Bora Eryilmaz I think you want to plot a duration array rather than a datetime array. As an example let's take 10 values randomly generated as a number of seconds between 0 and 3 hours.
s = sort(randi([0 seconds(hours(3))], 1, 10))
Create a duration array from s by calling seconds on it. seconds, minutes, hours, etc. can accept either a duration or a number and converts to the other type. I used it in both senses above; hours(3) to convert a number to a duration and seconds on the duration to returns the number of seconds in 3 hours.
t = seconds(s)
Let's change its Format. This doesn't change the data, just how it's displayed to the user.
t.Format = 'hh:mm:ss'
and now we can plot.
plot(t, 1:10, 'o-')
You can't zoom on the picture here on MATLAB Answers, but if you were to run this code in an interactive MATLAB session you could and you'd see the X axis update.
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!