MATLAB: How to get the first 10 characters of each cell in an array

celldatenum

I have a cell array (attached) containing dates that look like this:
'1999-01-02T18:24:37Z'
I'd like to get these dates into datenum format, which I can accomplish like this:
ct = char(t{2});
d = datenum(str2double(cellstr(ct(:,1:4))),str2double(cellstr(ct(:,6:7))),str2double(cellstr(ct(:,9:10))));
but that seems awfully convoluted and the str2double calls take a tremendous amount of time for long date lists. All I need are the year, month, and day in datenum format. Is there an elegant way to access only the first ten characters in the cell array t?

Best Answer

This is more readable
>> tic, sdn = cellfun( @(str) datenum( str(1:10), 'yyyy-mm-dd' ), t, 'uni',true ); toc
Elapsed time is 0.012129 seconds.
and this is an order of magnitude faster
tic
str = char(t);
sd2 = datenum( str(:,1:10), 'yyyy-mm-dd' );
toc
Elapsed time is 0.001011 seconds.
They return the same result :-)
And still a bit faster
tic
sd3 = datenum( t, 'yyyy-mm-dd' );
toc
Elapsed time is 0.000927 seconds.
>> all(sd2==sd3)
ans =
1
but is it documented?
>> datestr( datenum( '1999-01-12T18:13:45Z', 'yyyy-mm' ), 31 )
ans =
1999-01-01 00:00:00
>> datestr( datenum( '1999-01-12T18:13:45Z', 'yyyy-mm-ddTHH:MM:SSZ' ), 31 )
ans =
1999-01-12 19:13:45