Matlab datenum generation
I'm using dtstr2dtnummx because that is faster, but I faced a problem, this function is generation a different datenum, or maybe my date format is not correct
date = {'2011-03-13 23:59:59.100'}
out1 =datenum(date)
out2=dtstr2dtnummx(date,'yyyy-mm-dd HH:MM:SS.SSS')
and the outputs are:
date =
'2011-03-13 23:59:59.100'
out1 =
734575.999989583
out2 =
736开发者_开发问答281.960423495
I don't know where is the error
UPDATE
I found an answer, with the following dateformat I get the same result
dtstr2dtnummx(date,'YYYY-MM-dd HH:mm:ss.SSS')
But I don't know why like this, because I followed this tutorial, but I get the wrong output, If anybody know the answer please share it.
datenum
and dtstr2dtnummx
use different format strings: datenum
calls cnv2icudf
to convert "date format tokens to ICU date format tokens" (see help cnv2icudf
) which are usable with dtstr2dtnummx
. Note this includes swapping upper- and lowercase for MM (interchanging month and minutes). In other words, you used the documentation for datenum
to create a format string for dtstr2dtnummx
, which is undocumented and private.
To get the correct format string to use, you can call
cnv2icudf('yyyy-mm-dd HH:MM:SS.FFF')
resulting in
ans =
yyyy-MM-dd HH:mm:ss.SSS
which is quite close to the solution you found yourself.
Lesson learned: there is a reason why dtstr2dtnummx
is a private function - don't call it until you understand the wrapper datenum
;-).
BTW, out of curiosity: Did you really profile your code and found datenum
to be a major culprit?
精彩评论