matlab - "Failed on converting date string to date number" due to mixed of timestamp format -
somehow timestamp of raw data is,
a= {'12/5/2015 11:59:58 pm'; '13/5/2015'; % ideally '13/5/2015 12:00 am' '13/5/2015 12:00:01 am; '13/5/2015 12:00:01 am}
i not perform datenum due mixed of timestamp format where,
datenum(a,'dd/mm/yyyy hh:mm:ss pm')
gave me "failed on converting date string date number"
how go converting entities in (which has mixed timeformat) time number series?
in order find indices of ones use particular format, can use regular expressions.
take example longer format , match a
:
longformat = '^\d\d/(1?)\d/\d\d\d\d \d\d:\d\d:\d\d [ap]m$'; found = regexp(a,longformat) ans = [1] [] [1] [1]
so, can use anonymous function find of answers found:
foundlong = @(a,expr) cellfun(@(x) ~isempty(x),regexp(a,expr)); found = foundlong(a,longformat) found = 1 0 1 1
now have indices can use datenum()
specific format. example:
d(found) = datenum(a{found},'dd/mm/yyyy hh:mm:ss am');
caution: if start shorter expression,
shortformat = '^\d\d/(1?)\d/\d\d\d\d';
you'll match all of them. so, make sure add in end of sequence $
, match end of string:
shortformat = '^\d\d/(1?)\d/\d\d\d\d$';
Comments
Post a Comment