Filter löschen
Filter löschen

Ascending Sort in a cell of file names

1 Ansicht (letzte 30 Tage)
Henrique Amaral
Henrique Amaral am 4 Dez. 2013
Kommentiert: Henrique Amaral am 5 Dez. 2013
Dear friends,
I have a list of file names stored in a cell and I would like to sort this names. My cell variable is b:
Workspace: b < 15x1 cell>
b =
'p1-cimg.txt'
'p11-cimg.txt'
'p12-cimg.txt'
'p13-cimg.txt'
'p14-cimg.txt'
'p3-cimg.txt'
'p4-cimg.txt'
'p5-cimg.txt'
'p6-cimg.txt'
'p7-cimg.txt'
'p8-cimg.txt'
'p9-cimg.txt'
'p10-cimg.txt'
'p15-cimg.txt'
'p2-cimg.txt'
I would like perform an ascending sort and obtain:
b=
'p1-cimg.txt'
'p2-cimg.txt'
'p3-cimg.txt'
....
Anyone knows how to do that?
I tried use sort, but I got this error: >> sort(b) Error using sort Input argument must be a cell array of strings.
Thank you very much!

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 4 Dez. 2013
Given your b, try this:
num = cellfun(@(x) sscanf(x,'p%f-cimg.txt'),b) ;
[~,sortindex] = sort(num)
b(sortindex)
  1 Kommentar
Henrique Amaral
Henrique Amaral am 5 Dez. 2013
Thank you Jos! It works perfectly! I really appreciate your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 4 Dez. 2013
b ={'p1-cimg.txt'
'p11-cimg.txt'
'p12-cimg.txt'
'p13-cimg.txt'
'p14-cimg.txt'
'p3-cimg.txt'
'p4-cimg.txt'
'p5-cimg.txt'
'p6-cimg.txt'
'p7-cimg.txt'
'p8-cimg.txt'
'p9-cimg.txt'
'p10-cimg.txt'
'p15-cimg.txt'
'p2-cimg.txt'};
ii = regexp(b,'(?<=p)\d*','match');
[i0,i0] = sort(str2double([ii{:}]));
out = b(i0);
  1 Kommentar
Henrique Amaral
Henrique Amaral am 5 Dez. 2013
Thank you Andrei! It works perfectly as well! I really appreciate your help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices 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