MATLAB: Sorting a structure array

data importMATLABsortstructures

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

Best Answer

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