MATLAB Answers

0

Datenum not failiing as expected

Asked by Thor Andreas on 24 Jun 2014
Latest activity Answered by dpb
on 24 Jun 2014
Hi
I am using datenum to convert strings to serial datenum, using a specified string format. But I have a problem, datenum returns a value even if the string is not on the specified format. e.g
datenum('20140630-12022', 'yyyymmdd-HHMMSS')
returns a valid number even if the time field is a digit short. This even works
datenum('201406-120223', 'yyyymmdd-HHMMSS')
missing two of the digits of the date field. Is it possible to make datenum more strict, or is there other ways to verify the format?

  0 Comments

Sign in to comment.

Tags

1 Answer

dpb
Answer by dpb
on 24 Jun 2014

Build a verification function before passing to datenum -- something on the order of
>> ds={'20140630-12022';'201406-120223';'20140630-120224'};
>> cellfun(@(s) strfind(s,'-')==9 & length(s)==15,ds)
ans =
0
0
1
>>
This one checks for the proper length of date field and overall; hence the time field must be same length as well. Doesn't do anything about verifying reasonable values, of course.
regexp could be of use in verifying patterns, too, depending on how much qualifying you want to do.

  0 Comments

Sign in to comment.