datestr, add non-interpreted characters into format

5 Ansichten (letzte 30 Tage)
Vincent
Vincent am 30 Sep. 2011
Hi, I have two cell-arrays containing values like
7.345 * 10^5
7.344 * 10^5
2011-01-04_16h50m20s
2010-11-30_07h00m02s
I want to concatenate both and sort the Cell-Array by time (found a function "sortcell" in File Exchange). But the Array first has to be of the same format, of course.
How can i achieve this? I thought of converting the datenum-integers to date-strings but I didn't get how to write "h" or something in my string, especially with cellstrings.
Thank you in advance.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 30 Sep. 2011
data = {'2011-01-04_16h50m20s'
'2010-11-30_07h00m02s'}
date2 = cellfun(@(x)[x{:}],regexp(data,'[-_hms]','split'),'un',0);
[ign,ind] = sort(datenum(date2,'yyyymmddHHMMSS'));
out = data(ind)
EDIT
[ign,ind] = sort(datenum(regexprep(data,'[-_hms]',''),'yyyymmddHHMMSS'));
out = data(ind)

Weitere Antworten (1)

Vincent
Vincent am 30 Sep. 2011
Okay, seems like a cellfun or loop is neccessary. But I must admit: your solution is very tolerant against errors.
Didn't know yet that it is possible to abbreviate 'UniformOutput' by 'un'. Nice :)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by