How do I calculate the duration between two times?
3 views (last 30 days)
Show older comments
Hi,
I have two different data sets that each contain times in the format 'HH:mm:ss'.
results_start_array (see attached) contains a list of general start times in the first column (each representing the general start time of a participant). Each time indicates when a participant started their task.
results_start_end (see attached) is a cell array of cells, with 12 tables in each cell. Each table has two times. Each of these tables represents a sub-task. In each table, the first row is a start time and the second row is a end time of each respective sub-task.
My goal is to calcualate all the different durations between (for each participant):
- start time of sub-task 1 - general start time
- end time of sub-task 1 - general start time
- start time of sub-task 2 - general start time
- end time of sub-task 2 - general start time
- start time of sub-task 3 - general start time
- end time of sub-task 3 - general start time
- etc. ...
I tried this out by calculating only the start time of sub-task 1 - general start time using this code:
% Extract the start time from results_start_array
start_time = results_start_array(1);
% Extract the end time from the first row in the first table in the first cell of results_start_end
end_time_str = results_start_end{1}{1, 'Time'};
% Convert the end time string to datetime object
end_time = datetime(end_time_str{1}, 'InputFormat', 'HH:mm:ss');
% Convert the start time to datetime object assuming it is in the same day
start_time_dt = datetime(start_time, 'ConvertFrom', 'duration', 'Format', 'HH:mm:ss');
% Calculate the duration between the two times
time_duration = end_time - start_time_dt;
% Display the duration
disp(time_duration);
Which didnt seem to work. What am I doing wrong and how can I fix this?
Thx
0 Comments
Accepted Answer
Star Strider
on 16 Jun 2024
This seems to work —
load('results_start_array.mat')
whos('-file','results_start_array.mat')
load('results_start_end.mat')
whos('-file','results_start_end.mat')
r_start = results_start_array
r_start_end = results_start_end
r_start_end{1}{1}
r_start_end{end}{1}
for k1 = 1:numel(r_start)
% Q1 = numel(r_start_end{k1})
for k2 = 1:numel(r_start_end{k1})
% Q2 = r_start_end{k1}{k2}
ET{k1,k2} = r_start_end{k1}{k2}.Time - r_start(k1);
end
end
ET
r_start(1)
r_start_end{1}{1}
ET{1,1}
% ET{1,end}
% ET{end,1}
r_start(end)
r_start_end{end}{end}
ET{end,end}
I suggest that you check the results to be certain they make sense. However from my brief inspection of selected results, it doew what I believee you want.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!