MATLAB: Sorting the name field in dir command

commanddirfieldfilesfoldernamenatsortfilessortstruct

Hi all,
I have a problem when I'm trying to get the content of a folder. It gets the name of the files but not in order. For example, the files starts with X- where the X is a number from 1 to 320. My code gets the name's file in the order 1, 10, 100, etc… and I need it in order 1,2,3,4…320
This is my code
myFolder = 'C:\Users\U95511\Dropbox\UPF\Roberto Verino\Advantrack\Test15-130313\P12\A3\Results';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.xls');
xlsFiles = dir(filePattern);
I need to order the struct xlsFiles.
I tried with W=sort(xlsFiles.name) but it creates an array instead of another struct ordered.
Thanks for the help.
BR,
Raúl.

Best Answer

f= dir(filePattern);
xlsfiles={f.name}
xlsfiles=sort(xlsfiles)