简体   繁体   中英

How do i read and display multiple images within a folder in Matlab?

I have a folder containing 6 images and i want to display each of them in matlab. the images are saved as image01,image02....image06.

The output of the code is displaying only the first image multiple times. what am i missing??

a = dir('Example\*.png');
b = 'C:\Example\';


for i=1:length(a) %where a is the path to the image folder
   fileName = strcat(b,a(i).name);
   disp(fileName);% this allows me to see the names in text.
   Image = imread('C:\Example/Image01.png');
   figure, imshow(Image);
end

This loop works and does tell me the name of each image 1 by 1 using the disp(filename) so its not a syntax error.

Thanks for the help.

This worked!

a = dir('Example\*.png');
b = 'C:\Example\';


for i=1:length(a) %where a is the path to the image folder
   fileName = strcat(b,a(i).name);
   Name = a(i).name;
   disp(fileName);% this allows me to see the names in text.
   Image = imread(Name);
   figure, imshow(Image);
end

Actually, the dir function should return all what you need in order to locate your files properly (and rebuild their respective path too), without declaring an auxiliary variable to hold the target path:

files = dir('C:\...\MyFolder\*.png');

for i = 1:numel(files)
   file = files(i);
   filename = fullfile(file.folder,file.name);

   disp(filename);

   img = imread(filename);
   figure();
   imshow(img);
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM