Borrar filtros
Borrar filtros

Ascending Sort in a cell of file names

1 visualización (últimos 30 días)
Henrique Amaral
Henrique Amaral el 4 de Dic. de 2013
Comentada: Henrique Amaral el 5 de Dic. de 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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by