How to plot an array with an hourly time stamp without starting at 00:00:00?
Ältere Kommentare anzeigen
I have a file with basically two colums: time and signal. Time is recorded with the format hh:mm:ss.SSS. First datapoint is at 17:09:47.818 and last one at 08:36:11.706 the next day. I do not have any other info about days, months or anything.
Is there a simple way to plot time vs. signal, and that the x-axis shows the time with the format hh:mm:ss.SSS? If I do simply
figure,plot(T,S)
I get the following graphic:

Which is not ordered in time as I expect. I used the trick of plotting only the signal as:
figure,plot(S)
and defining the xticklabels with the time as I need it, but of course this is a "lie", because as you can see, the data is correct ordered, but the Data Tip does not show the time of the observed signal:

Do you know how can I properly draw a graphic like the second one, but that correctly uses time as in the first figure, and starts at the correct time and not at 00:00?
Thank you all in advance!
2 Kommentare
dpb
am 15 Dez. 2021
If the sample times are uniformly spaced, you can just create a time vector datetime from start to end with the correct number of points.
If the data aren't evenly space, then you need to find the point in the record at which the date change occurs and add 24 hrs to a duration variable that is the difference from the beginning. You can then set the initial time to the origin again by adding that interval magnitude.
Or, convert to a datetime which includes an aribtrary date as well as time -- again you'll have to add the 24 hr point at which time the new day begins. You can set the display format of the datetime axis to not display the date.
Alternatively, use datenum instead, you can add arbitrary date and hours to its representation without worrying about whether a datetime value has a date or not and without the necessity to keep duration and datetime variables separately. That along with datetick will liet you show the plot; the Data Tip, however, will still display the underlying datenum value unless you write a custom callback routine.
Federico Geser
am 16 Dez. 2021
Akzeptierte Antwort
Weitere Antworten (1)
Adam Danz
am 15 Dez. 2021
It sounds like you just need to set the datetime format of the x-tick labels and the x-axis limit.
Use datetime-values for x-coordinates and set datetime format of xticks
xtickformat(axisHandle,'HH:mm:ss.SSS')
% OR
axisHandle.XAxis.TickLabelFormat='HH:mm:ss.SSS';
Set the XLim
axisHandle.XLim = [min(dtvals), max(dtvals)]; % dtvals are your datetime values
2 Kommentare
His problem is he doesn't have input enough for a valid datetime variable without building one -- when he converts the input into the datetime, he gets the default date (today) added to the time portion and consequently the two days are merged together, resulting in the first plot where plotted by timeofday and the earlier data that begins at 17:00 hours of day 1 is plotted after the data from day 2 which carries on from midnight of the prior day.
Adam Danz
am 15 Dez. 2021
I see. Thanks for the clarification.
Kategorien
Mehr zu Dates and Time finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

