tags:

views:

472

answers:

4

Hello,

I have bmp images in image folder on my computer. I named it from 1.bmp to 100.bmp.

And I want to read these images like this:

for i=1:100
    s='C:\images'+i+'.bmp';
    A=imread(s);
end

But Matlab gave an error. How can I implement this?

+3  A: 

let me guess. you don't have files named C:\images1.bmp. Oh, that's not the error you're getting, but it will be the next one, once you follow ypnos's advice.

SilentGhost
: -)
Jason S
Good catch on the additional error, but ypnos' answer actually doesn't work. ;)
gnovice
@gnovice: the error message will be the same ;)
SilentGhost
@SilentGhost: You're right. His answer won't throw an error... it will just mangle the file name even further, leading to the same error. =)
gnovice
Oooops, my fault! Didn't think it through. I will delete my answer.
ypnos
+8  A: 

You can use sprintf function

s = sprintf('c:\images%d.bmp', i);
A = imread(s);

You can read more about string handling in matlab here

Can
The \ inside the sprintf format string should be escaped as '\\'.
Andrew Janke
+4  A: 

Create s in the following way:

s = ['C:\images\' int2str(i) '.bmp'];

Also, your loop will simply keep overwriting A, so you will instead have to make it a cell array to store all 100 images. Do this outside your loop:

A = cell(1,100);

and then load your images in the loop like so:

A{i} = imread(s);
gnovice
+1 for spotting the A being overwritten
second
A: 
imgfiles=dir('c:\images\*.*');
for k=1:length(imgfiles)
  ...
end 
jawad

related questions