datestr, add non-interpreted characters into format

5 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-9-30
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)

更多回答(1 个)

Vincent
Vincent 2011-9-30
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 :)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by