Filter löschen
Filter löschen

Plotting x-axis time in dd hh mm ss format

163 Ansichten (letzte 30 Tage)
Calum am 4 Apr. 2023
Bearbeitet: Bora Eryilmaz am 11 Apr. 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,

Akzeptierte Antwort

Bora Eryilmaz
Bora Eryilmaz am 4 Apr. 2023
Bearbeitet: Bora Eryilmaz am 11 Apr. 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);
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));
ax = gca;
ax.XTickLabelRotation = 45;
All the zoom, pan, etc, behaviour would continue to be functional.
  5 Kommentare
Bora Eryilmaz
Bora Eryilmaz am 5 Apr. 2023
See updated code.
Calum am 11 Apr. 2023
Thank you! This now seems to be working! The only problem is that I can't seem to get milliseconds as this format 'SSS' doesnt seem to be recognised on the 2020b version I am using, but the hh:mm:ss form is good enough - thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 5 Apr. 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))
s = 1×10
321 715 1846 3672 4857 5154 7973 8208 10024 10397
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)
t = 1×10 duration array
321 sec 715 sec 1846 sec 3672 sec 4857 sec 5154 sec 7973 sec 8208 sec 10024 sec 10397 sec
Let's change its Format. This doesn't change the data, just how it's displayed to the user.
t.Format = 'hh:mm:ss'
t = 1×10 duration array
00:05:21 00:11:55 00:30:46 01:01:12 01:20:57 01:25:54 02:12:53 02:16:48 02:47:04 02:53:17
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.

Community Treasure Hunt

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

Start Hunting!

Translated by