Matlab does plot datetime data (only hours) on wrong day

12 views (last 30 days)
Hello,
I have data from two sensors that I want to plot in the same window to compare differences in my X value.
I have them both stored in tables and use the datetime "HH:mm:ss.SS" format.
For example:
sensor1.Time = ['12:41:50.00'; '12:41:51.00'; ...]
sensor1.x = [0.01; 0.02; 0.02; ...]
and
sensor2.Time = ['12:42:05.46'; '12:42:05.49'; ...]
sensor2.x = [0.01; 0.02; 0.01; ...]
When I plot the data using
plot(sensor1.Time, sensor1.x) it plots the data in a window and adds todays date.
However, when I plot the other table it adds a random date (here: 15.07.2022).
If I plot both using hold on; the data then looks like this which is not very helpful:
How can I fix this date error and have the data aligned on one day?
Best Regards,
Renan

Accepted Answer

Star Strider
Star Strider on 17 Jul 2022
Without having the data to experiment with, my only suggestion is to plot them after converting them to duration arrays (that itself is not as straightforward as I would like it to be, especially since duration arrays are limited to one day in length) —
Time1 = datetime(2022,7,15) + minutes(0:1439).'
Time1 = 1440×1 datetime array
15-Jul-2022 00:00:00 15-Jul-2022 00:01:00 15-Jul-2022 00:02:00 15-Jul-2022 00:03:00 15-Jul-2022 00:04:00 15-Jul-2022 00:05:00 15-Jul-2022 00:06:00 15-Jul-2022 00:07:00 15-Jul-2022 00:08:00 15-Jul-2022 00:09:00 15-Jul-2022 00:10:00 15-Jul-2022 00:11:00 15-Jul-2022 00:12:00 15-Jul-2022 00:13:00 15-Jul-2022 00:14:00 15-Jul-2022 00:15:00 15-Jul-2022 00:16:00 15-Jul-2022 00:17:00 15-Jul-2022 00:18:00 15-Jul-2022 00:19:00 15-Jul-2022 00:20:00 15-Jul-2022 00:21:00 15-Jul-2022 00:22:00 15-Jul-2022 00:23:00 15-Jul-2022 00:24:00 15-Jul-2022 00:25:00 15-Jul-2022 00:26:00 15-Jul-2022 00:27:00 15-Jul-2022 00:28:00 15-Jul-2022 00:29:00
Data1 = sin(2*pi*(0:numel(Time1)-1)/720);
Time2 = datetime(2022,07,18) + minutes(0:1439).'
Time2 = 1440×1 datetime array
18-Jul-2022 00:00:00 18-Jul-2022 00:01:00 18-Jul-2022 00:02:00 18-Jul-2022 00:03:00 18-Jul-2022 00:04:00 18-Jul-2022 00:05:00 18-Jul-2022 00:06:00 18-Jul-2022 00:07:00 18-Jul-2022 00:08:00 18-Jul-2022 00:09:00 18-Jul-2022 00:10:00 18-Jul-2022 00:11:00 18-Jul-2022 00:12:00 18-Jul-2022 00:13:00 18-Jul-2022 00:14:00 18-Jul-2022 00:15:00 18-Jul-2022 00:16:00 18-Jul-2022 00:17:00 18-Jul-2022 00:18:00 18-Jul-2022 00:19:00 18-Jul-2022 00:20:00 18-Jul-2022 00:21:00 18-Jul-2022 00:22:00 18-Jul-2022 00:23:00 18-Jul-2022 00:24:00 18-Jul-2022 00:25:00 18-Jul-2022 00:26:00 18-Jul-2022 00:27:00 18-Jul-2022 00:28:00 18-Jul-2022 00:29:00
Data2 = cos(2*pi*(0:numel(Time1)-1)/720);
figure
plot(Time1, Data1)
figure
plot(Time2, Data2)
figure
plot(Time1,Data1)
hold on
plot(Time2, Data2)
hold off
Time1.Format = 'HH:mm:ss';
Dur1 = duration(string(Time1)); % Convert To 'duration' Array
Time2.Format = 'HH:mm:ss';
Dur2 = duration(string(Time2)); % Convert To 'duration' Array
figure
plot(Dur1, Data1)
hold on
plot(Dur2, Data2)
hold off
Your data will likely work for a duration array, however if they go for longer than one calendar day, it might be necessary to convert the time values only to datenum vectors and plot the data against that instead.
.
  5 Comments
Steven Lord
Steven Lord on 17 Jul 2022
@Star Strider The behavior you described is correct, but it's not due to any issue with duration. It's due to the fact that the datetime data covers multiple days.
midnight = datetime('today')
midnight = datetime
17-Jul-2022
dt = midnight + minutes([-5, 5])
dt = 1×2 datetime array
16-Jul-2022 23:55:00 17-Jul-2022 00:05:00
dt.Format = 'HH:mm:ss'
dt = 1×2 datetime array
23:55:00 00:05:00
If you extracted the duration data from the datetime array with timeofday, you could check for any large jumps. This would indicate crossing midnight and could be resolved by adding 24 hours.
tod = timeofday(dt)
tod = 1×2 duration array
23:55:00 00:05:00
d = [0, diff(tod)]
d = 1×2 duration array
00:00:00 -23:50:00
tod(d < 0) = tod(d < 0) + hours(24)
tod = 1×2 duration array
23:55:00 24:05:00
check = midnight - days(1) + tod
check = 1×2 datetime array
16-Jul-2022 23:55:00 17-Jul-2022 00:05:00
The check array looks like it matches the dt array.

Sign in to comment.

More Answers (2)

the cyclist
the cyclist on 17 Jul 2022
The most fundamental fix to your issue is to explicitly use the modern datetime format for your times. Then, downstream functions that use them as inputs can be manipulated to control the display output.
Also, rather than "MATLAB randomly does something for some reason", one can generally find the exact behavior in the documentation for each function.

Renan Deuter
Renan Deuter on 17 Jul 2022
Another way that worked and might actually even be easier is this:
1) Create an new variable with a custom date
day = repmat(datetime(2022, 6, 30), length(sensor1.x), 1)
2) Merge two variables into one datetime array following this guide
-> Basically we reformat both columns so both can fit hours and a date.
sensor1.Time.Format = 'dd.MM.uuuu HH:mm:ss.SS';
day.Format = 'dd.MM.uuuu HH:mm:ss.SS';
This sets our day variable to: 30.06.2022 00:00:00.00
and the Time column to: 15.07.2022 14:04:32.23
The we copy the time to the day variable
sensor1.Time = day + timeofday(sensor1.Time);

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!