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