Hi,
I understand that you want to sum multiple clock times.
In the following solution, I have assumed that the input argument times is a cell array of time strings in the format hh:mm:ss & the function returns the sum of all times as a time string in the same format.
To sum multiple clock time in MATLAB assuming they reset every 24 hours, you can perform the following steps:
- Convert each time string to a numerical value in hours.
- If the resulting value is greater than 24, subtract 24 to ensure that it is within the range of [0, 24).
- Add the numerical values of all times.
- If the sum is greater than 24, subtract 24 to ensure that it is within the range of [0, 24).
- Convert the resulting numerical value back to a time string.
You may refer to the below mentioned example code for summing multiple clock times in MATLAB:
function total_time = sum_clock_times(times)
[hours, minutes, seconds] = strread(times{i},'%d:%d:%d');
time_in_hours = hours + minutes/60 + seconds/3600;
time_in_hours = time_in_hours - 24;
total_time = total_time + time_in_hours;
total_time = total_time - 24;
hours = floor(total_time);
minutes = floor((total_time - hours) * 60);
seconds = floor((((total_time - hours) * 60) - minutes) * 60);
total_time = sprintf('%02d:%02d:%02d', hours, minutes, seconds);
Regards,
Raghav Bansal