5

I have a problem reading multiple images in Matlab from a folder. I want to read with their original name (with the command imread because are multiband). The names of the images are like '2001_01', '2001_02'. This is my code:

myPath= 'C:\images\'; %'
a=dir(fullfile(myPath,'*.tif'));
fileNames={a.name};

And then...

for k = 1:length(fileNames)
    filename = [fileNames(k).name];  
    I = imread(filename);
end

But it doesn't work and I don't know how to save and imread each one individually. Does somebody know how can I do it? Really thanks in advance,

1
  • Does it return the full file path?
    – user349026
    Apr 24, 2013 at 11:27

3 Answers 3

6
  1. Regarding the first problem:

    But it doesn't work...

    Just assign the output of dir directly into fileNames (without brackets):

    fileNames = dir(fullfile(myPath, '*.tif'));
    
  2. Regarding the second problem:

    ... I don't know how to save and imread each one individually.

    it seems that you need a cell array to store all images in a single collection. First, define the cell array to have the right size:

    C = cell(length(fileNames), 1);
    

    and then store each image into a different cell:

    for k = 1:length(fileNames)
        filename = fileNames(k).name;
        C{k} = imread(filename);
    end
    

    To access any image in the cell array C later, use curly braces ({}). For instance, the second image is accessed as follows: C{2}.

2
  • 1
    Thanks Eitan for your quickly reply! It works! And it's possible that on the C{k} the list would appear the names of the files? The list show the "row x column x band". Apr 24, 2013 at 12:02
  • 1
    You can make C a cell array with two columns, one for the stored images and one for the corresponding filenames. To do that, initialize the cell array like so: C = cell(length(filenames), 2); and in each iteration assign the cell values as following: C{k, 1} = filename; C{k, 2} = imread(filename);
    – Eitan T
    Apr 24, 2013 at 13:23
2

Instead of

 fileNames={a.name};

Try

fileNames = arrayfun( @(x) fullfile( myPath, x.name ), a, 'UniformOutput', false );

Then, in the loop you can access the k-th file name as

I = imread( filenames{k} );
2
  • Thanks Shai, but I receive the following message: ??? Improper index matrix reference. Apr 24, 2013 at 12:05
  • @user1578688 have you set k?
    – Shai
    Apr 24, 2013 at 12:08
1

Does it return the full file path? fileNames(k).name ? or just the actual file name? You might need to append myPath with filename taking care of slashes as well

fileName = strcat(myPath, fileName)

Then do the imread, make sure you have looked at the slashes once contactenated

2
  • better use fullfile than strcat in this context.
    – Shai
    Apr 24, 2013 at 11:33
  • Thanks Wajih! Yes, it return the full file path and all the files. Apr 24, 2013 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.