From: Pasan on 29 Jun 2010 06:02 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 29 Jun 2010 07:45 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 29 Jun 2010 14:59 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 29 Jun 2010 17:23 "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 29 Jun 2010 20:10 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
|
Next
|
Last
Pages: 1 2 3 Prev: Find loop within vector Next: export uitable selected cells in matlab |