MATLAB: Calculate duration from cell with date hours with Matlab 2013a

datedurationr2013atime

I have a cell array of more than 16700 rows and 3 column as in the example below:
'01/01/2014' '12:00:00' 'AM'
'01/01/2014' '06:00:00' 'AM'
'01/01/2014' '10:00:00' 'AM'
'01/01/2014' '12:00:00' 'PM'
'09/04/2014' '11:00:00' 'PM'
I would like to calculate the duration of two chosen dates and time (for example the last row-first raw=number of days, hours and seconds). My big problem is that I have a R2013a version of Matlab. So I can't use DateTime, I tried this:
newA = cellfun(@(s) strsplit(s, ' '), DateTime, 'UniformOutput', false);
DateTimeSep = vertcat(newA{:});
%%Calculs de durée et récupération de données
date=datenum(DateTimeSep(:,1),'mm/dd/yyyy');
d=datestr(date(1256,1)-date(1,1), 'dd')
Time=DateTimeSep(10,2)-DateTimeSep(1,2)% I didn't try to consider the AM PM problem yet....
I get d=04 (which is false) and for time I get: "Undefined function 'minus' for input arguments of type 'cell'"

Best Answer

% [Don't use this - see [EDITED 2] for a nicer solution!]
D = {'01/01/2014' '12:00:00' 'AM'; ...
'01/01/2014' '06:00:00' 'AM'; ...
'01/01/2014' '10:00:00' 'AM'; ...
'01/01/2014' '12:00:00' 'PM'; ...
'09/04/2014' '11:00:00' 'PM'};
DateV = datevec(strcat(D(:, 1), '#', D(:, 2)), 'mm/dd/yyyy#HH:MM:SS');
IsPM = strcmpi(D(:, 3), 'PM');
pstPM = (DateV(:, 4) < 12 & IsPM);
DateV(pstPM, 4) = DateV(pstPM, 4) + 12; % 12:01PM ==> 12:01, 1:01PM ==> 13:01
midAM = (DateV(:, 4) == 12 & ~IsPM);
DateV(midAM, 4) = 0; % 12:01AM ==> 00:01
DateN = datenum(DateV);
Now the time difference can be calculated by a simple subtraction.
datevec(DateN(end) - DateN(1)) % [EDITED, DateTimeN -> DateN]
[EDITED 2] No, the conversion of the time difference to months and years is not meaningful: It is not clear if the "month" has 28, 29, 30 or 31 days, if it concerns the difference between dates. Better:
Duration = DateN(end) - DateN(1);
DurationDays = fix(Duration);
Tmp = rem(Duration, 1); % Time
DurationHour = floor(Tmp * 24);
DurationMin = floor(rem(Tmp * 1440, 60));
DurationSec = round(rem(Tmp * 86400, 60));
Sorry, this is too ugly! Nicer:
D = {'02/14/2014', '10:02:04', 'AM'; ...
'02/26/2014', '01:00:00', 'PM'};
DateN = datenum(strcat(D(:, 1), '#', D(:, 2), '#', D(:, 3)), ...
'mm/dd/yyyy#HH:MM:SS#AM');
Duration = DateN(end) - DateN(1);
DDays = fix(Duration);
[~,~,~, Dhour, Dmin, Dsec] = datevec(rem(Duration, 1));