MATLAB: Datevec() and datenum() fail intermittently with custom format string (MATLAB R2010a)

date conversiondatenumdatevecMATLAB

Successive calls to datevec() and datenum() with the same argument alternately success and fail. Any ideas on what might be causing this or something I can do to help debug it?
Below is a command-line example, but it happens in functions and scripts too. Note: sometimes (e.g. after a fresh start of MATLAB) it works perfectly every time. (It will probably work perfectly for you, too.)
K>> start_timeTxt
start_timeTxt =
2013-07-16 10:34:59
K>> class(start_timeTxt)
ans =
char
% Nothing abnormal in time string
K>> double(start_timeTxt)
ans =
50 48 49 51 45 48 55 45 49 54 32 49 48 58 51 52 58 53 57
K>> datevec(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
??? Error using ==> dtstr2dtvecmx
Failed on converting date string to date number.
Error in ==> datevec at 124
y = dtstr2dtvecmx(t,icu_dtformat);
% Try it again...
K>> datevec(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
ans =
2013 7 16 10 34 59
% And again...
K>> datevec(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
??? Error using ==> dtstr2dtvecmx
Failed on converting date string to date number.
Error in ==> datevec at 124
y = dtstr2dtvecmx(t,icu_dtformat);
% And a third time...
K>> datevec(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
ans =
2013 7 16 10 34 59
% Is datenum() broken too? Expectedly, yes...:
K>> datenum(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
??? Error using ==> datenum at 182
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
% but like datevec(), it alternately works too…
K>> datenum(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
ans =
7.3543e+005
K>> datenum(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
??? Error using ==> datenum at 182
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
K>> datenum(start_timeTxt, 'yyyy-mm-dd HH:MM:SS')
ans =
7.3543e+005
% Try it with literals
>> datenum('2013-07-16 10:34:59', 'yyyy-mm-dd HH:MM:SS')
??? Error using ==> datenum at 182
DATENUM failed.
Caused by:
Error using ==> dtstr2dtnummx
Failed on converting date string to date number.
>> datenum('2013-07-16 10:34:59', 'yyyy-mm-dd HH:MM:SS')
ans =
7.3543e+005
% In case you are wondering:
K>> which datenum, which datevec
C:\Program Files\MATLAB\R2010a\toolbox\matlab\timefun\datenum.m
C:\Program Files\MATLAB\R2010a\toolbox\matlab\timefun\datevec.m
K>> ver
————————————————————————————-
MATLAB Version 7.10.0.499 (R2010a) MATLAB License Number: ••••
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)
Java VM Version: Java 1.6.0_12-b04 with Sun Microsystems Inc. Java HotSpot™ 64-Bit Server VM mixed mode
————————————————————————————-
K>> memory
Maximum possible array: 4553 MB (4.775e+009 bytes) *
Memory available for all arrays: 4553 MB (4.775e+009 bytes) *
Memory used by MATLAB: 627 MB (6.578e+008 bytes)
Physical Memory (RAM): 4030 MB (4.225e+009 bytes)
* Limited by System Memory (physical + swap file) available.

Best Answer

This is a known bug, see http://www.mathworks.com/support/bugreports/details.html?rp=622845. You find downloadable workarounds there also.
It is always worth to take a look into the list of known bugs in case of problems.
Related Question