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

1 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2023-6-18
编辑:Stephen23 2023-6-18
"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 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by