MATLAB: Computing average absolute time from date strings without years

average timeMATLABtimetime series

Hi there, I have a very large table with a column of (12 hr) times saved as strings in a cell array, and a column of folder names also saved as strings in a cell array. So for example, say my table was called 'data', then if I type:
K>> data.folder
ans =
1150×1 cell array
{'160115/f1/f2/f3'}
I can access a string directly by typing:
K>> data.folder{1}
ans =
'160115/f1/f2/f3'
The first part of the folder string has the date in YYMMDD format. Another column in the table has the time in a string (12 hour clock):
K>> data.end_time{1}
ans =
'11:24:17 AM'
The actual table is enormous. I have some index for each row which fits into some supercategory (eg taken on some date), and I want to average the absolute time for each row in that category. How can I do this?
I have a brute force solution for the first part which works but I don't like how it's a loop. I would prefer to do it in a more compact way. Anyways, here it is:
exact_times = datetime(ramsey_data.end_time);
date = ramsey_data.folder;
for i=1:numel(exact_times) ymd = date{i};
ymd = ymd(1:6);
year = ['20' ymd(1:2)];
month = ymd(3:4);
day = ymd(5:6);
exact_times(i).Year = str2num(year);
exact_times(i).Month = str2num(month);
exact_times(i).Day = str2num(day);
end
I still need to find a way to average the resulting values in exact_times properly. I want to select only some of the rows in the table to average. If I could use accumarray I would use something like: average_time = accumarray(subs, exact_times, [], @mean); but I can't use that on datetime variables

Best Answer

It's really just three lines to do this. I've shown scalar values, but it works just as well on string or cellstr arrays:
>> date = '160115/f1/f2/f3';
>> time = '11:24:17 AM';
>> date = extractBefore(date,'/')
date =
'160115'
>> date = datetime(date,'InputFormat','yyMMdd')
date =
datetime
15-Jan-2016
>> time = timeofday(datetime(time,'Format','hh:mm:ss aa'))
time =
duration
11:24:17
>> dt = date + time
dt =
datetime
15-Jan-2016 11:24:17