Datenum not failiing as expected

1 vue (au cours des 30 derniers jours)
Thor Andreas
Thor Andreas le 24 Juin 2014
Réponse apportée : dpb le 24 Juin 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?

Réponses (1)

dpb
dpb le 24 Juin 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.

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by