How can I display multiple images in one figure window using for loop?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Accepted Answer
% 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 Comments
Stephen23
on 31 Jul 2019
It would be simpler just to specify the dir file name with wildcard and extension:
content = dir(fullfile(folder,'*.png'));
Lauren Pitts
on 31 Jul 2019
Thank you, Adam. This worked perfectly!
Adam Danz
on 31 Jul 2019
Glad I could chip in!
Elif
on 1 Sep 2021
How can I write image names?
Adam Danz
on 1 Sep 2021
Use title() or text().
Elif
on 3 Sep 2021
Thank you.
Baba Mohammed
on 5 Oct 2021
Thanks
image-pro
on 19 Apr 2022
Showing error
Undefined function 'endsWith' for input arguments of type 'cell'.
Error in Untitled9 (line 10)
isExt = endsWith(allfiles,['.',ext],'IgnoreCase',true); % req. r2016b or later
https://www.mathworks.com/help/matlab/ref/endswith.html
Adam Danz
on 19 Apr 2022
@image-pro, what matlab release are you using?
image-pro
on 20 Apr 2022
matlab 2015
Adam Danz
on 20 Apr 2022
Check out the comment next to the endsWith line. This solution requires MATLAB R2016b or later. Always take a moment to view the code when you're copying/pasting it.
I've updated my answer to include an alternative for pre-2016b. See the bottom of the answer.
image-pro
on 22 Apr 2022
thank you
Stephen23
on 22 Apr 2022
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.
More Answers (0)
Categories
Find more on Environment and Settings in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)