MATLAB Answers

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

2 views (last 30 days)
Jim Tonti
Jim Tonti on 27 Jul 2013
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.
  2 Comments
per isakson
per isakson on 27 Jul 2013
Have you checked the format string before the call to dtstr2dtvecmx and dtstr2dtnummx, respectively? A mismatch in the format string causes this error:
>> datevec(start_timeTxt, 'yyyy-mmdd HH:MM:SS')
Error using dtstr2dtvecmx
Failed on converting date string to date number.
Error in datevec (line 118)
y = dtstr2dtvecmx(t,icu_dtformat);

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Jul 2013
Edited: Jan on 27 Jul 2013
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.
  2 Comments
Jan
Jan on 28 Jul 2013
@Jim: This bug has been fixed for the following releases and for your release also - you find the required new functions in the bug report.
It seems to be a simple problem with an uninitialized static variable and it is strange that such a bug was not found before the release.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!