Sorting a structure array

2 visualizzazioni (ultimi 30 giorni)
FabioP
FabioP il 26 Lug 2019
Modificato: per isakson il 27 Lug 2019
I have used a previous code that creates a structure array with the name of several subfolder where i want to import data
% Path of the main folder :
yourpath = 'D:\Dropbox\...';
% Get all the subfolders
ContentInFold = dir(yourpath);
SubFold = ContentInFold([ContentInFold.isdir]); % keep only the directories
% Loop on each folder
FColLocal = [];
for i = 3:length(SubFold) % start at 3 to skip . and ..
filetoread = fullfile(yourpath,SubFold(i).name,'FCol_LocalForce.out'); % <- this is the "Fcol_LocalForce.txt" file of the ith subfolders
% then type your code to read the text files and store in one variable
FColLocal{end+1} = dlmread(filetoread, ' ', [10 0 1008 0]); % the format depends of your files
end
In the SubFold structure array, thats where i would like to sort the field 'name' with a diferend order of the determined by Matlab.
The problem is that the field 'name' is formed with characteres of the type:
'1._0.005._0.2_.4._2'
'10._0.005._0.2_.16._4' ,
'100._0.002._0.5_.4._16'
The field name corresponds to subfolder name (except the first 2 rows),
Duvida.PNG
but I would like to change to (as it is ordered in the windows folder)
'1._0.005._0.2_.4._2' ,
'2._0.005._0.2_.4._4'
'3._0.005._0.2_.4._8'
...
where only the first character of the name field controles the sorting (ascending).
Thanks
  2 Commenti
Jon
Jon il 26 Lug 2019
Could you please further clarify. Please make a short example list of the filenames in the order that they are returned by directory and then for those same filenames please list how you would like them sorted. I think you have tried to do that above, but I am confused by your listing for example of '3._0.005._0.2_.4._8', I don't see that filename anywhere in the screenshot you have, and don't really know what you are trying to show.
FabioP
FabioP il 26 Lug 2019
The List of the filenames (sub-folders) that are returned by directory in the use the previous code are the ones in the figure above:
'1._0.005._0.2_.4._2'
'10._0.005._0.2_.16._4' ,
'100._0.002._0.5_.4._16'
'101._0.002._0.5_.8._2'
and continue down the column to a total of 252 folders (from a parametric study) thats why they dont appear all in the figure.
The first character of filename folder (x._y._z._k._m) , the x, represent the couting of the subfolders, and it shoud start in:
'1._0.005._0.2_.4._2' ,
'2._0.005._0.2_.4._4'
'3._0.005._0.2_.4._8'
....
as it is in the windows folder (from 1 to 252) sorted by name:
imagem.PNG
Hope it helps to understand the problem.

Accedi per commentare.

Risposta accettata

per isakson
per isakson il 27 Lug 2019
Modificato: per isakson il 27 Lug 2019
Hint:
This script sorts with regard to the leading group of digits (converted to numeric)
%%
SubFold = { '1._0.005._0.2_.4._2'
'10._0.005._0.2_.16._4'
'100._0.002._0.5_.4._16'
'101._0.002._0.5_.8._2'
'2._0.005._0.2_.4._4'
'3._0.005._0.2_.4._8'
};
%%
cac = regexp( SubFold, '^\d+', 'match' );
num = cellfun( @(c) str2double(c), cac );
[~,ixs] = sort( num );
%%
SubFold( ixs, 1 )
it returns
ans =
6×1 cell array
{'1._0.005._0.2_.4._2' }
{'2._0.005._0.2_.4._4' }
{'3._0.005._0.2_.4._8' }
{'10._0.005._0.2_.16._4' }
{'100._0.002._0.5_.4._16'}
{'101._0.002._0.5_.8._2' }
>>
Search the File Exchange for sort natural

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by