How do I calculate the duration between two times?
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?
ThxHi,
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 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 time, duration, subtract, duration array, error, calculate duration MATLAB Answers — New Questions