How do I calculate the duration between two times?

6 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 16 Jun 2024
This seems to work —
load('results_start_array.mat')
whos('-file','results_start_array.mat')
Name Size Bytes Class Attributes results_start_array - 216 duration
load('results_start_end.mat')
whos('-file','results_start_end.mat')
Name Size Bytes Class Attributes results_start_end 25x1 338300 cell
r_start = results_start_array
r_start = 25x1 duration array
13:19:21 17:55:46 15:30:17 17:01:24 09:37:57 12:28:59 14:06:50 15:33:28 09:40:56 12:35:40 14:16:12 15:42:35 09:43:18 10:59:57 14:01:05 09:29:51 11:06:01 12:30:32 15:25:37 09:47:49 11:08:48 12:37:49 09:43:26 11:18:47 13:52:57
r_start_end = results_start_end
r_start_end = 25x1 cell array
{1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell} {1x12 cell}
r_start_end{1}{1}
ans = 2x1 table
Time ________ 13:21:26 13:21:56
r_start_end{end}{1}
ans = 2x1 table
Time ________ 13:52:57 13:53:27
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
ET = 25x12 cell array
Columns 1 through 10 {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} Columns 11 through 12 {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration} {2x1 duration}
r_start(1)
ans = duration
13:19:21
r_start_end{1}{1}
ans = 2x1 table
Time ________ 13:21:26 13:21:56
ET{1,1}
ans = 2x1 duration array
00:02:05 00:02:35
% ET{1,end}
% ET{end,1}
r_start(end)
ans = duration
13:52:57
r_start_end{end}{end}
ans = 2x1 table
Time ________ 14:02:12 14:02:42
ET{end,end}
ans = 2x1 duration array
00:09:15 00:09:45
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
lil brain
lil brain on 16 Jun 2024
Great! Yes I checked the values and calculated by hand and it all checks out. Great help thx!

Sign in to comment.

More Answers (0)

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!