フィルターのクリア

How can I display multiple images in one figure window using for loop?

10 ビュー (過去 30 日間)
Lauren Pitts
Lauren Pitts 2019 年 7 月 30 日
コメント済み: Stephen23 2022 年 4 月 22 日
Is there a way for me to be able to read/display 100 images (0001.png - 0100.png) without having to type them all manually like below? All 100 images are in a folder if that helps (C:\Users\pittsl\Desktop\Matlab\train\cup).
files = {'0001.png', '0002.png','0003.png','0004.png','0005.png',};
for K = 1 : 5
this_image = imread(files{K});
ax = subplot(1, 5, K);
imshow(this_image, 'Parent', ax);
end

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 30 日
編集済み: Adam Danz 2022 年 4 月 20 日
% Inputs: folder and file extension
% Will plot all files with the chosen extension in the chosen folder
folder = 'C:\Users\pittsl\Desktop\Matlab\train\cup';
ext = 'png'; %extension, no dot
% Get list of all files
content = dir(folder);
allfiles = {content.name};
isExt = endsWith(allfiles,['.',ext],'IgnoreCase',true); % req. r2016b or later https://www.mathworks.com/help/matlab/ref/endswith.html
files = allfiles(isExt);
% Determine subplot layout
nImages = numel(files);
dimN = ceil(sqrt(nImages));
dimM = ceil(nImages/dimN);
nrows = min(dimN, dimM);
ncols = max(dimN, dimM);
% Plot them
for K = 1:nImages
this_image = imread(fullfile(folder,files{K}));
ax = subplot(nrows,ncols, K);
imshow(this_image, 'Parent', ax);
end
For MATLAB releases prior to R2016b, replace the endsWith function with this,
isExt = ~cellfun('isempty',regexpi(allfiles,['.',ext,'$']));
Alternative
To show thumbnail images of all image files within a folder or images with specified extensions, see showImageThumbnails from the File Exchange. Image tiles will be numbered and a table is returned that defines the path to each tile.
folder = 'C:\Users\pittsl\Desktop\Matlab\train\cup';
ext = '.png'; % include dot
showImageThumbnails(folder, ext)
  13 件のコメント
image-pro
image-pro 2022 年 4 月 22 日
thank you
Stephen23
Stephen23 2022 年 4 月 22 日
Simpler:
folder = 'C:\Users\pittsl\Desktop\Matlab\train\cup';
ext = 'png'; %extension, no dot
% Get list of all files
content = dir(fullfile(folder,sprintf('*.%s',ext)));
files = {content.name};
.. etc.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by