I have a folder with 1500 images. I need to process the first 200 images(ie, 1 to 200). I used for loop it. But when i run the code i starts from 1,2,3 ... 100. then instead if going for 101th image, its value changes to 1001 , 1002... and so on.
1 view (last 30 days)
Stephen23 on 8 Feb 2016
Edited: Stephen23 on 26 Apr 2021
You have discovered ASCII sorting is not the same as human sorting.
The easiest solution is to use my FEX submission natsortfiles. It fixes the order of an ASCII sort to take into account any numeric values in the strings. Note that it requires the file natsort to work!
It simply sorts a cell array of filenames into the order you want:
S = dir('*.jpg');
S = natsortfiles(S);
for k = 1:200 % first 200 files
F = S(k).name;
... your code
Note that this answer assumes that your files are sequentially numbered without any gaps. If this is not true, then you need to give more information on the filenaming logic.