Azzera filtri
Azzera filtri

Ascending Sort in a cell of file names

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

Andrei Bobrov
Andrei Bobrov il 4 Dic 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 Commento
Henrique Amaral
Henrique Amaral il 5 Dic 2013
Thank you Andrei! It works perfectly as well! I really appreciate your help.

Accedi per commentare.

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by