Borrar filtros
Borrar filtros

create matrix (rx1) with the data obtained from the for loop

1 visualización (últimos 30 días)
Alberto Acri
Alberto Acri el 17 de Jun. de 2023
Editada: Stephen23 el 18 de Jun. de 2023
Hi. I have several numbered folders inside one folder.
I would like to sort the obtained dfolders structure by the name field.
I tried this way but nothing:
folder = pwd;
d = dir(folder);
dfolders = d([d(:).isdir]);
dfolders = dfolders(~ismember({dfolders(:).name},{'.','..'}));
matrix = zeros(10,1);
for numb = 1:10
column_name = dfolders(numb).name;
matrix = [matrix; column_name];
end

Respuesta aceptada

Stephen23
Stephen23 el 18 de Jun. de 2023
Editada: Stephen23 el 18 de Jun. de 2023
"Maybe it is because there are numbers in the name field (not in sequence)? I don't think so."
Yes, it is exactly because of that.
SORT sorts only by character code, not by the value of any numbers in the text.
If you want to sort those directories by name alphanumerically then download NATSORTFILES from here:
and use it like this:
S = dir(folder);
S(~[d.isdir]) = [];
S = natsortfiles(S,[],'rmdot','noext')
That assumes that the numbers are integers only**. If they include decimal fractions, negative/positive signs, etc, then you will need to specify an appropriate regular expression for the 2nd input, following the NATSORTFILES documentation.
** the simpler approach is to ensure sufficient leading zeros and then just use SORT.

Más respuestas (0)

Categorías

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

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by