Plotting x-axis time in dd hh mm ss format

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.

 Accepted Answer

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 Comments

Hi Bora thanks for your response I really appreciate it - though I am still having some slight trouble with this. I am simply trying to plot in the form 'HH:MM:SS:FFF', without the days or months.
So far, I have managed to convert my time array(in seconds) to a char array in the format above. I have done this by saying that:
dayconvert = secondsconvert/86400; %60*60*24=86400
time = datestr(dayconvert, 'HH:MM:SS:FFF')
However, I now don't know how to get this on an interactive plot with the above on the x axis in a numeric-type format.
I tried what you said above using my time array (in seconds)on the x against my y data, and then using:
ax = gca;
ax.XAxis.TickLabelFormat = 'HH:MM:SS:FFF';
However this doesnt work for me, do you have any additional advice for my task?
Thanks,
C.
See the updated code. The format should be "HH:mm:ss.SSS" to show milliseconds.
Hi Bora - I understand slightly more what you mean, and this is helping. I think i am nearly there.
To explain it better though, I would not like to consider any live dates at all. I have my time array which is an array of 9950x1 double, with each entry being the format e.g. 4.3860e+04 seconds. I would simply like to change this into the format "HH:mm:ss.SSS" (without considering any days or dates) on the x axis and plot this, though I keep trying the method above and can't get this to work and keep getting met with errors saying that TickLabelFormat must be a duration format.
I do not see how to get around this - is there maybe a way to 'zero' the datetime so that only the array of seconds is converted to "HH:mm:ss.SSS" for use?
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!

Sign in to comment.

More Answers (1)

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.

Asked:

on 4 Apr 2023

Edited:

on 11 Apr 2023

Community Treasure Hunt

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

Start Hunting!