How to read images in a folder
Show older comments
I am a student and I need to find the code to read the images in a file in order to do cropping. In here I named my images as
user001-01.bmp
user001-02.bmp etc.
I need help for this because I'm a beginner to MATLAB.
7 Comments
Maria Ramirez
on 28 Oct 2018
Edited: Walter Roberson
on 12 Sep 2020
oof=cd; % this corresponds to your current folder
nf=(fullfile(cd,' put here the name of your folder')); %this means the new folder where you created it
cd(nf) names=dir; names=names(1:end); % it depends on where the folder starts
hh=length(names);
for idx=1:hh
v(idx).names=names(idx).name;
end % the new vector that got the fields
for idx=1:hh
name=v(idx).names;
img=imread(name);
% you can delete this, this only graphs the images
imshow(img)
drawnow %
% Here you put the respectively code, depends on what do you want to do with the images
end %this returns you to the current folder, where you started
cd(oof)
Created by SMC
Stephen23
on 28 Oct 2018
@Maria Ramirez: using cd like that is slow and makes debugging harder, and should be avoided: it is recommended to simply use fullfile to access data files in other directories, to make code more efficient and easier to debug. Note that in your code there is no point in transferring the filenames from the structure names to the structure v, or even in using two separate for loops. You should also use a match string when calling dir, as otherwise it will return the names of all files in the directory, even if they are not the correct type for processing.
So a simpler, more efficient, more robust version of your code would be something like this:
D = 'directory where the files are saved';
S = dir(fullfile(D,'name*.jpg')); % pattern to match filenames.
for k = 1:numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
imshow(I)
S(k).data = I; % optional, save data.
end
You might like to take a look at the examples in the documentation:
Lenin Falconi
on 2 Nov 2018
Thanks it worked fine. I was struggling with a code using ls which worked fine in Windows but it didn't work in Ubuntu because it delivers a char of 1xall chars in directory...
vikram punia
on 3 Nov 2020
Thanks, Really helped.
Rijal Fadilah Tsalis
on 17 Jan 2021
its very helpfull
naila
on 24 Jun 2023
hi,
i have some jpeg images in a folder (saved in C drive),using imread command in matlab 2018 but its not working.
please help me out.
Image Analyst
on 24 Jun 2023
If you have any more questions, then attach your image and code to read it in with the paperclip icon after you read this:
Do it in your own new question rather than here in @Nilushi Dissanayake's question. And "not working" is so vauge as to be unanswerable. Explain what that means, like give us ALL the red text.
Accepted Answer
More Answers (5)
Sebastian
on 25 Jan 2011
There might be different ways to do this. An easy one is
for a = 1:10
filename = ['user001-' num2str(a,'%02d') '.bmp'];
img = imread(filename);
% do something with img
end
4 Comments
Nilushi Dissanayake
on 25 Jan 2011
Tong Liang
on 23 Sep 2016
Yeah, this is what I would usually do. But, this method won't work if the filenames of the images don't fit into a certain pattern
artprakasa
on 13 Mar 2017
yeaayy.. thanks, good idea.
Liu Sam
on 4 Mar 2018
Thank you! This is the exact answer I want!
George
on 24 Sep 2016
1 vote
3 Comments
Harpreet Kaur
on 11 Sep 2017
but it only works in 2015b and above. is there any alternative to ImageDatastore?
Walter Roberson
on 11 Sep 2017
To be slightly more accurate, the imageDatastore function needs R2016a or later, but the imageDatastore class was introduced in R2015b and objects could be created by calls to datastore.
Walter Roberson
on 11 Sep 2017
Before R2015a, the closest equivalent was the dataset array from the Statistics toolbox.
DGM
on 24 Jun 2023
0 votes
There are already many examples that I don't need to repeat, but I have my own ways of doing things. I'm only throwing this here because this is a defacto reference question.
Pay particular note to the latter part of the answer.
Anushrita
on 16 Jul 2025
0 votes
% Step 1: Set path to your image folder
imageFolder = 'path_to_your_folder'; % e.g., 'C:\Users\YourName\Pictures\dataset'
% Step 2: Create an imageDatastore
imds = imageDatastore(imageFolder, ...
'IncludeSubfolders', true, ... % Set to true if you have class folders
'LabelSource', 'foldernames'); % Labels images based on folder names
% Step 3: View basic info
disp('Number of images:');
disp(numel(imds.Files));
disp('Class labels:');
disp(unique(imds.Labels));
% Step 4: Read and show one image (optional)
img = readimage(imds, 1);
imshow(img);
title(sprintf('Label: %s', string(imds.Labels(1))));
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!