MATLAB: Make matlab read the files in order

outputread file

I am using a code to read and calculate mass center. but the code read file with this oder:
Now reading C:\Users\shape_01.dat
Now reading C:\Users\shape_010.dat
Now reading C:\Users\shape_0100.dat
Now reading C:\Users\hape_0101.dat
so i get wrong orders for my output results.
because i need matlab to process my file in normal order: 1 2 3 4 4 5 …
please is there any suggestion how to solve that?

Best Answer

Copying from the examples in the NATSORTFILES documentation**:
..
S = dir(fullfile(myFolder,'shape_0*.txt'));
N = natsortfiles({S.name}); % sort the filenames!
for k = 1:numel(N)
baseFileName = N{k};
..
** Which is why I wrote that documentation, including several examples of sorting the output from DIR.