Sorting a structure array
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Jon
2019 年 7 月 26 日
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.
採用された回答
per isakson
2019 年 7 月 27 日
編集済み: per isakson
2019 年 7 月 27 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!