MATLAB: The problem about dir function

dir function sequence

hi every one:
when I use dir function to get the same type files of a file, (the file name is 10.png, 20.png, 50.png, 100.png, 200.png, 300.png, 1000.png), then I find the sequence is 10.png, 100.png, 1000.png, 20.png, 200.png…..
but what I want to get is the sequence like 10.png, 20.png, 50.png, 100.png, 200.png, 300.png, 1000.png
so please give me some suggestion or any help
thanks a lot

Best Answer

The most practical method is to use "0010" instead of "10". Then the numerical order equals the lexicographical order.
While there are some FEX submissions for "natural sorting", you can sort the names manually also:
List = dir('*.png');
Name = {List.name};
S = sprintf('%s,', Name{:}); % '10.png,100.png,1000.png,20.png, ...'
D = sscanf(S, '%d.png,'); % [10; 100, 1000; 20; ...]
[sortedD, sortIndex] = sort(D); % Sort numerically
sortedName = Name(sortIndex); % Apply sorting index to original names