Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 18 Jun 2023
Edited: Stephen23 on 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.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!