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

3 views (last 30 days)
Federico Geser
Federico Geser on 15 Dec 2021
Edited: Adam Danz on 17 Dec 2021
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 Comments
Federico Geser
Federico Geser on 16 Dec 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!

Sign in to comment.

Accepted Answer

dpb
dpb on 15 Dec 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 Comments
Adam Danz
Adam Danz on 17 Dec 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')

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 15 Dec 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 Comments

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!