MATLAB: How to sort the order given by ‘dir’ command ??

commanddigital image processingdirimage analysisimage processing

I am reading the images from a folder by using the below code
[folder1] = uigetdir();
f=dir(folder1);
for k=1:size(f,1)-2
file_n=f(k+2).name;
end
The images in the folder are "test_base1", "test_base2", …. but the order in which 'dir' taking is "test_base1","test_base10","test_base2".. I understood that the command is taking ascii-betical order.. I have gone through the previous questions and answers. It seems complicated. Can any one give a good solution to sort the order of Images after using "dir"

Best Answer

file={'test_base1','test_base10','test_base2'}
[idx,idx]=sort(cellfun(@(x) str2num(char(regexp(x,'\d*','match'))),file))
new_file=file(idx)