Home > database >  How to read multiple images and store them on MATLAB?
How to read multiple images and store them on MATLAB?

Time:07-20

So this is my code so far,

clear;clc;
path = uigetdir(); %open directory
if isequal(path,0) 
   disp('User selected Cancel');
return
else
   disp(['User selected ', (path)]); 
end

% pathPattern = fullfile(path); 
theFiles = dir(path); %Get list of all files with desired pattern
for k = 3 :5  %Change length when needed
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(theFiles(k).folder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName)

end

a = imread(fullFileName);

My problem is that imread only reads one of the images from my directory, I need it to read all the images and store them in a cell array. I've tried multiple answers from the internet and none of them has worked so far.

CodePudding user response:

Store in a cell array inside the loop since it allows for images of different sizes. If you know in advance that the images will be of the same size, using a regular numeric array will be much faster.

clear, clc
path = uigetdir(); % open directory
if isequal(path,0) 
   disp('User selected Cancel');
return
else
   disp(['User selected ', (path)]); 
end

theFiles = dir(path);       % Get list of all files with desired pattern
for k = 3:length(theFiles)  % Change length when needed
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(theFiles(k).folder, baseFileName);
    fprintf(1,'Now reading %s\n', fullFileName)
    a{k-2} = imread(fullFileName);
end

a
  • Related