Datenum not failiing as expected

1 次查看(过去 30 天)
Thor Andreas
Thor Andreas 2014-6-24
回答: dpb 2014-6-24
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?

回答(1 个)

dpb
dpb 2014-6-24
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.

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by