Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: slow with converting date strings to datenum?

Tags:

matlab

With 100,000 date strings to convert to datenums, Matlab is really slow... 20 seconds!! Is there a faster way of doing this?

%strDay  
%strTime

dblDate = zeros(1, nLines);
for i = 1 : nLines
    dblDate(j) = datenum([strDay{i}, ' ', strTime{i}], 'yy.mm.dd HH:MM:SS.FFF');
    j = j + 1;
end
like image 291
jdl Avatar asked May 27 '26 23:05

jdl


1 Answers

Internally, datenum does a large amount of fiddling around to process many different date formats. Then it calls the internal function datenummx with different input arguments depending on the date format and the syntax with which you called datenum.

If you know ahead of time which syntax you will be calling datenum with, and which date format you will be using, you can call datenummx directly. Since datenummx is a built-in MEX file, and avoids all the overhead processing, it is typically vastly faster.

Take a look into datenum (type edit datenum) to find out the appropriate way to call datenummx for your application.

(Plus, do all the vectorization that other answers have suggested).

like image 84
Sam Roberts Avatar answered May 30 '26 11:05

Sam Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!