# Sum calendar Duration array

8 views (last 30 days)
Rose on 9 Jan 2020
Answered: Guillaume on 9 Jan 2020
Hi Guys,
I have two arrays in the datetime format. 1 array with the starting points of an event, a 2nd array with the end points of the same event. Using the "between" function I have determined the times between the start and end of each event. Now resulting in 1 array with calendarDuration values.
My goal is to find the total duration, thus summing up all values in the calendarDuration array. However... The sum function does not work for this format, see the code resulting in the error below. Any help on how to sum up the values with calendarDuration format?
Error using sum
Invalid data type. First argument must be numeric or logical.
threshold=1.5;
exceed_RH=Druk_RH>1.5; %logical array for threshold exceedance
difference_RH=diff(exceed_RH);
start_operation_RH=difference_RH==1;
time_start_operation_RH=time(start_operation_RH); % Returns array with datetimes of start of operation
end_operation_RH=difference_RH==-1;
time_end_operation_RH=time(end_operation_RH); %Returns array with datetimes of end of operation
dt=between(time_end_operation_RH,time_start_operation_RH); %time between start and end, calendarDuration format
total_uptime_RH=sum(dt)

Guillaume on 9 Jan 2020
Are you actually interested in getting the result as a calendarDuration as opposed to plain duration? Indeed, for some reason sum is not implemented fo calendarDuration. It is for duration though.
I assume that you actually don't want calendarDuration, which is useful if you want to express elapsed time in terms of years, months or weeks but not so useful for ellapsed time in hours, minutes, seconds. For that you're better served with plain duration. In that case:
dt = time_end_operation_RH - time_end_operation_RH; %plain duration array
total_uptime_RH = sum(dt);
If you do want a calendarDuration result, you'll have to perform the sum in a loop:
dt = between(time_end_operation_RH, time_start_operation_RH);
total_uptime_RH = 0;
for idt = 1:numel(dt)
total_uptime_RH = total_uptime_RH + dt(idt);
end