How to plot an array with an hourly time stamp without starting at 00:00:00?

43 visualizaciones (últimos 30 días)
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 comentarios
dpb
dpb el 15 de Dic. de 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
Federico Geser el 16 de Dic. de 2021
Thank you dpb for you help. So, there is no other way but to manually find the date changing point, am I right? I will try to follow your advices, in the mean time, I let you the file with the data attached. I had to delete most of the data because of the 5 MB limit.
Thank you in advance!

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 15 de Dic. de 2021
You can build complete datetime variables from the input by something like--
dt1=datetime("yesterday")+duration(T(1)); % the first time is before midnight previous day
dt2=datetime(T(end)); % datetime defaults to "today" for missing date
You can use above logic to apply to the sections of the T vector that belong to which day by logical indexing to find the first location where the time value turns from 23:59.59.SSS to 00:00:00:SSS
Like always, if you would attach the file, folks would have something to play with without having to make up an example dataset, always an incentive to actually do something.
  5 comentarios
dpb
dpb el 16 de Dic. de 2021
Editada: dpb el 16 de Dic. de 2021
Clever, indeed, for what it does...but it doesn't get rid of the "Dec 15, 2021-Dec 16,2021" displayed date range on the datetime axis leaving only one looking at the time-of-day on the plot without the extraneous clutter/meaningless reference date which is the wart left here with way the DatetimeRuler object is constrained to work in not understanding one can have/want an absolute time of day wiithout really having a real day with which it is associated.
What TMW would have you do here is create a duration variable from the beginning of the first date -- I suppose that isn't any more difficult than creating the datetime, but it certainly is far less intuitive.
ADDENDUM: Unless there's something else I'm not seeing going on here???
Adam Danz
Adam Danz el 17 de Dic. de 2021
Editada: Adam Danz el 17 de Dic. de 2021
I was recently exploring the differences between datetick and xtickformat. The prefered method of formatting datetime tick is by using datetime values along with xtickformat but if you use datetick instead, it omits the trailing date tag.
datetick vs xtickformat demo
dt = datetime('yesterday') + minutes(0:5:200)';
data = nan(size(dt));
figure
tiledlayout(2,1)
nexttile
plot(dt, data)
xtickformat('HH:mm');
title('xtickformat')
subtitle('contains date stamp')
nexttile
plot(dt, data)
datetick('x', 'HH:MM', 'keepticks') % note change in case
title('datetick')
subtitle('does not contain date stamp')

Iniciar sesión para comentar.

Más respuestas (1)

Adam Danz
Adam Danz el 15 de Dic. de 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 comentarios
dpb
dpb el 15 de Dic. de 2021
Editada: dpb el 16 de Dic. de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by