我如何在 Matlab 中读取和显示一个文件夹中的多个图像?

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

我有一个包含 6 张图像的文件夹,我想在 matlab 中显示每张图像。 图像保存为 image01,image02....image06.

代码的输出多次只显示第一张图片。 我错过了什么??

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

这个循环有效,并且确实使用 disp(filename) 逐一告诉我每个图像的名称,所以它不是语法错误。

感谢您的帮助。

这成功了!

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

实际上,dir 函数应该 return 正确定位文件(并重建它们各自的路径)所需的所有内容,而无需声明辅助变量来保存目标路径:

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