yalcin
yalcin

Reputation: 541

Reading images from file in MATLAB

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?

Upvotes: 3

Views: 11747

Answers (6)

Karthick Rajan
Karthick Rajan

Reputation: 396

>  for i=1:100
>      s=strcat('C:\images',num2str(i),'.bmp');
>      try                                                  
>        A=imread(s);
>      catch
>      end 
>    end

here i am using num2str which is used for convert the data type of number to string and i am using try for aviod the error because if file is not there then it will aviod that error.

Upvotes: 0

Asadullah
Asadullah

Reputation: 11

Add your folder to the matlab directory path and run the following commands.

files=dir('*.bmp') for k=1:numel(files) I=imread(files(k).name); end

I am using these commands to read the image files.

Upvotes: 1

jawad
jawad

Reputation: 21

imgfiles=dir('c:\images\*.*');
for k=1:length(imgfiles)
  ...
end 

Upvotes: 2

gnovice
gnovice

Reputation: 125854

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);

Upvotes: 5

user111095
user111095

Reputation:

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

Upvotes: 9

SilentGhost
SilentGhost

Reputation: 319511

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.

Upvotes: 3

Related Questions