From: Pasan on
I have a large(1 million elements) cell array of datetime strings called 'dt_strings' formatted in datetime format no. 31.

I want to convert this cell array to an array of standard matlab serial datetime numbers for easier manupulation.

Which of the following method is best? Or are they equal in functionality. Is there a better way to do this?

1. dt_numbers=cellfun(@(x)datenum(x,31),dt_strings);

or

2. dt_numbers=datenum(dt_strings,31);

I feel both these methods take an unreasonably long time to finish.
From: Jan Simon on
Dear Pasan,

> 1. dt_numbers=cellfun(@(x)datenum(x,31),dt_strings);
> 2. dt_numbers=datenum(dt_strings,31);
>
> I feel both these methods take an unreasonably long time to finish.

You could try DateConvert:
http://www.mathworks.com/matlabcentral/fileexchange/25594
This can be called with DATESTR(x, 0) strings only, but you can get the underlying idea to build something like this:

nD = numel(dt_cell);
dt_number = zeros(nD, 1);
for iC = 1:nD
x = dt_Cell{iC} - '0';
dt_number(iC) = datenummx( ...
x(1)*1000 + x(2)*100 + x(3)*10 + x(4), ... % year
x(6)*10 + x(7), ... % Month
x(9)*10 + x(10), ... % Day
x(12)*10 + x(13), x(15)*10 + x(16), x(18)*10 + x(19));
end
The simplicity of the method cries for a C-Mex function. But perhaps the already gain speed is enough.

Good luck, Jan
From: Jan Simon on
Dear Pasan,

> > 1. dt_numbers=cellfun(@(x)datenum(x,31),dt_strings);
> > 2. dt_numbers=datenum(dt_strings,31);

> 3.
> function dt_number = DateStr31ToNum(dt_cell)
> nD = numel(dt_cell);
> dt_number = zeros(nD, 1);
> for iC = 1:nD
> x = dt_cell{iC} - '0'; % ### Typo in former message
> dt_number(iC) = datenummx( ...
> x(1)*1000 + x(2)*100 + x(3)*10 + x(4), ... % year
> x(6)*10 + x(7), ... % Month
> x(9)*10 + x(10), ... % Day
> x(12)*10 + x(13), x(15)*10 + x(16), x(18)*10 + x(19));
> end

Timings on my 1.5GHz Pentium-M, WinXP, Matlab 2009a, for 10'000 dates:
CELLFUN: 40.47 sec
DATEUM: 4.85 sec
M-Function: 0.20 sec
Mex: 0.0088 sec

I'm going to publish the Mex soon, Jan
From: Pasan on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message
> Timings on my 1.5GHz Pentium-M, WinXP, Matlab 2009a, for 10'000 dates:
> CELLFUN: 40.47 sec
> DATEUM: 4.85 sec
> M-Function: 0.20 sec
> Mex: 0.0088 sec
>
> I'm going to publish the Mex soon, Jan

Thanks a lot Jan!

I will also switch to your m-function and post speed improvements.
From: Jan Simon on
Dear Pasan,

> > CELLFUN: 40.47 sec
> > DATEUM: 4.85 sec
> > M-Function: 0.20 sec
> > Mex: 0.0088 sec

The Mex is on its way to the FEX.
Unfortunately testing the Mex failed, because I cannot get Matlab to create valid test data due to a bug:
datenum(datestr(now, 30), 30)

Jan