MATLAB Answers

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.

3 views (last 30 days)
start:1
stop:200
for i = start : stop
XX
end
But i need to continue from 101, 102,... after 100. Kindly help me identify the problem.

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 8 Feb 2016
Edited: Stephen Cobeldick on 8 Feb 2016
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');
N = natsortfiles({S.name});
for k = 1:200 % first 200 files
name = N{k};
... your code
end
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.

  2 Comments

jeffin
jeffin on 8 Feb 2016
sir, the problem with me is the following,
My code is as follows,
srcFiles = dir('E:\New Folder\*.tif'); % the folder in which images exists
start = 1 ;
end = 200;
for i = start : end
filename = strcat('E:\New Folder\',srcFiles(i).name);
I = imread(filename);
figure, imshow(I);
end
for
i = 1 => srcFiles(i).name = 1.tif
i = 2 => srcFiles(i).name = 2.tif
i = 3 => srcFiles(i).name = 3.tif
and so on... but when
i = 101 => srcFiles(i).name = 1001.tif and
i = 102 => srcFiles(i).name = 1002.tif
But i need this to read 101.tif and 102.tif for i=101 and 102.
Kindly help me.
Stephen Cobeldick
Stephen Cobeldick on 8 Feb 2016
Yes, I know what you want to do, that is why I answered your question, telling you how to solve your problem. Did you read my answer? I can see that you didn't use it in your code. Here is one way of solving your task using my answer:
myPath = 'E:\New Folder';
S = dir(fullfile(myPath,'*.tif'));
N = natsortfiles({S.name});
for k = 1:200
myFile = fullfile(myPath,N{k});
I = imread(myFile);
figure;
imshow(I);
end
Note:
  • you will need to download the two functions, just as I told you to.
  • I changed the loop variable from i to k, because in MATLAB i is the imaginary unit.
  • I used fullfile to create the path strings, which is much more robust than using strcat.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Feb 2016

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 9 Feb 2016
Neither of these are really suitable for sorting files as they include the file extension character in the sort. My FEX submission natsorfiles correctly considers the file-names and file-extensions separately.
Image Analyst
Image Analyst on 9 Feb 2016
Stephen, you should notice that first link I gave actually talks about your submission as the "Pick of the Week".

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!