create matrix (rx1) with the data obtained from the for loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Acri
am 17 Jun. 2023
Bearbeitet: Stephen23
am 18 Jun. 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
0 Kommentare
Akzeptierte Antwort
Stephen23
am 18 Jun. 2023
Bearbeitet: Stephen23
am 18 Jun. 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.
0 Kommentare
Weitere Antworten (0)
Siehe auch
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!