Sorting a structure array
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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),
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
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.
Risposta accettata
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
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!