datestr, add non-interpreted characters into format

5 vues (au cours des 30 derniers jours)
Vincent
Vincent le 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.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 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)

Plus de réponses (1)

Vincent
Vincent le 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 :)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by