Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 17 Juin 2023
Modifié(e) : Stephen23 le 18 Juin 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

Réponse acceptée

Stephen23
Stephen23 le 18 Juin 2023
Modifié(e) : Stephen23 le 18 Juin 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.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by