Duration of time values for separate datasets and plotting on x-axis

I have the following data sets that have in the x-axis time in "HH:mm:ss". Each data set has different numbers of "times" but they are all within a 2hr duration.
I want to plot this data in terms of duration on the same axes.
Sorry this is the update data.
set0_time=["085440","091530","092000","093020","100000","100020","101325"];
set0_ydata=[30 50 60 72 82 85 88];
H_set0 = str2double(extractBefore(set0_time(:),3));
M_set0 = str2double(extractBetween(set0_time(:),3,4));
S_set0 = str2double(extractAfter(set0_time(:),4));
D_set0 = duration(H_set0,M_set0,S_set0);
t_minutes_set0 = minutes(D_set0);
set1_time = ["121101","121105","130001","133025","140000"];
set1_ydata=[40 50 60 70 89];
H_set1 = str2double(extractBefore(set1_time(:),3));
M_set1 = str2double(extractBetween(set1_time(:),3,4));
S_set1 = str2double(extractAfter(set1_time(:),4));
D_set1 = duration(H_set1,M_set1,S_set1);
t_minutes_set1 = minutes(D_set1);
tdiff_minutes_set1 = t_minutes_set1 - t_minutes_set0(1:5); %There is an issue selecting the first five of set0 as the shift is not accurate
tdiff_shift_set1 = t_minutes_set1-tdiff_minutes_set1;
set2_time = ["103235","110010","120000","130130"];
set2_ydata=[37.8 70 85.7 93.6];
H_set2 = str2double(extractBefore(set2_time(:),3));
M_set2 = str2double(extractBetween(set2_time(:),3,4));
S_set2 = str2double(extractAfter(set2_time(:),4));
D_set2 = duration(H_set2,M_set2,S_set2);
t_minutes_set2 = minutes(D_set2);
tdiff_minutes_set2 = t_minutes_set2 - t_minutes_set0(1:4); %There is an issue selecting the first four of set0 as the shift is not accurate
tdiff_shift_set2 = t_minutes_set2-tdiff_minutes_set2;
figure(15)
scatter(t_minutes_set0,set0_ydata,'Marker','o', 'DisplayName','set0') %updated this line
hold on;
scatter(tdiff_shift_set1,set1_ydata,'Marker','*', 'DisplayName','set1')
scatter(tdiff_shift_set2,set2_ydata,'Marker','+', 'DisplayName','set2')
hold off
xlabel('Duration');
ylabel('Data');
legend
I'm not sure how to get all the data on the same plot but in the same "duration" of time.
Any help is appreciated.

 Accepted Answer

set0_time=["085440","091530","092000","093020","100000","100020","101325"];
set0 = set0_time;
set0_ydata=[30 50 60 72 82 85 88];
H_set0 = str2double(extractBefore(set0(:),3));
M_set0 = str2double(extractBetween(set0(:),3,4));
S_set0 = str2double(extractAfter(set0(:),4));
D_set0 = duration(H_set0,M_set0,S_set0);
t_minutes_set0 = minutes(D_set0);
% *snip the other data sets*
figure(1)
% scatter(set0_time,set0_ydata,'Marker','o', 'DisplayName','set0')
You didn't intend to use the string array set0_time in this scatter call, did you? I'll show what that would do after what I believe is the correct code.
scatter(D_set0, set0_ydata, 'Marker', 'o', 'DisplayName', 'set0') % Changed first input
figure
scatter(set0_time,set0_ydata,'Marker','o', 'DisplayName','set0') % won't work
Error using scatter (line 68)
Input arguments must be numeric, datetime, duration, or categorical.

5 Comments

Sorry I update the code above.
You are correct that the plot won't work.
I think there are two issues:
1) Unable to acurately "normalize" the time data against the first time data set, as the length of the sets are different.
2) I want the time data to be in duration not "HH:mm:ss" that's why I chose to convert the time to "minutes" for all of them, but that may not be correct.
Thanks
I actually update the code again:
scatter(t_minutes_set0,set0_ydata,'Marker','o', 'DisplayName','set0') %updated this line
Now the code plots, but the duration is off for set1 and set2 time values.
My apologies for all the updates.
1) Unable to acurately "normalize" the time data against the first time data set, as the length of the sets are different.
What does "normalize" mean to you in this context? Let's ignore the ydata for right now, until I understand what you want to do.
set0_time=["085440","091530","092000","093020","100000","100020","101325"];
d0 = convert(set0_time)
d0 = 1x7 duration array
08:54:40 09:15:30 09:20:00 09:30:20 10:00:00 10:00:20 10:13:25
set1_time = ["121101","121105","130001","133025","140000"];
d1 = convert(set1_time)
d1 = 1x5 duration array
12:11:01 12:11:05 13:00:01 13:30:25 14:00:00
set2_time = ["103235","110010","120000","130130"];
d2 = convert(set2_time)
d2 = 1x4 duration array
10:32:35 11:00:10 12:00:00 13:01:30
So how are the elements of d0, d1, and d2 related? It looks like you want each element to be treated as the number of minutes since the corresponding element of d0 (except for d0 itself.) Is that correct?
2) I want the time data to be in duration not "HH:mm:ss" that's why I chose to convert the time to "minutes" for all of them, but that may not be correct.
In the example above, d0 is a duration array with its Format set to "hh:mm:ss". So I'm not sure what you mean by 'in duration not "HH:mm:ss"'. Do you want it as a plain double number representing a certain number of seconds/minutes/hours/some other time period?
function dt = convert(t)
hms = double(t);
s = mod(hms, 100); % least significant two digits
hm = (hms-s)/100; % hm is an integer after the division
m = mod(hm, 100); % middle two significant digits of hms
h = (hm-m)/100; % again, by the way m was constructed h is an integer after division
dt = duration(h, m, s);
end
Okay, so do you mean you want each of set0, set1, and set2 to start at 0? In that case don't combine the data sets. Just subtract the first element of each set from the rest of that same set.
set0_time=["085440","091530","092000","093020","100000","100020","101325"];
d0 = convert(set0_time)
d0 = 1x7 duration array
08:54:40 09:15:30 09:20:00 09:30:20 10:00:00 10:00:20 10:13:25
d0_start = d0 - d0(1)
d0_start = 1x7 duration array
00:00:00 00:20:50 00:25:20 00:35:40 01:05:20 01:05:40 01:18:45
Note that the first element is 0.
d0_minutesElapsed = minutes(d0_start)
d0_minutesElapsed = 1×7
0 20.8333 25.3333 35.6667 65.3333 65.6667 78.7500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Do the same for the second set.
set1_time = ["121101","121105","130001","133025","140000"];
d1 = convert(set1_time)
d1 = 1x5 duration array
12:11:01 12:11:05 13:00:01 13:30:25 14:00:00
d1_start = d1 - d1(1)
d1_start = 1x5 duration array
00:00:00 00:00:04 00:49:00 01:19:24 01:48:59
d1_minutesElapsed = minutes(d1_start)
d1_minutesElapsed = 1×5
0 0.0667 49.0000 79.4000 108.9833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And the third.
set2_time = ["103235","110010","120000","130130"];
d2 = convert(set2_time)
d2 = 1x4 duration array
10:32:35 11:00:10 12:00:00 13:01:30
d2_start = d2 - d2(1)
d2_start = 1x4 duration array
00:00:00 00:27:35 01:27:25 02:28:55
d2_minutesElapsed = minutes(d2_start)
d2_minutesElapsed = 1×4
0 27.5833 87.4167 148.9167
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or were you looking more for the difference between corresponding elements of the same set?
d0 % Reminder of the values from d0
d0 = 1x7 duration array
08:54:40 09:15:30 09:20:00 09:30:20 10:00:00 10:00:20 10:13:25
d0_spacing = [0, diff(d0)]
d0_spacing = 1x7 duration array
00:00:00 00:20:50 00:04:30 00:10:20 00:29:40 00:00:20 00:13:05
By this, the second element of d0 is just under 21 minutes after the first element and the third element is 4 and a half minutes after the second. I added the 0 at the start because the output of diff for many types is one element shorter than the input and the first element of d0 is 0 minutes after the first element.
function dt = convert(t)
hms = double(t);
s = mod(hms, 100); % least significant two digits
hm = (hms-s)/100; % hm is an integer after the division
m = mod(hm, 100); % middle two significant digits of hms
h = (hm-m)/100; % again, by the way m was constructed h is an integer after division
dt = duration(h, m, s);
end
Thank you! This is what I needed.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Asked:

on 24 Sep 2024

Commented:

on 24 Sep 2024

Community Treasure Hunt

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

Start Hunting!