How to read images in a folder

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
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
@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:
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...
Thanks, Really helped.
its very helpfull
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.
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.

Sign in to comment.

 Accepted Answer

Nilushi -
We won't be able to hand you a complete answer, but can give some pointers. Here's a code snippet that shows common patterns for reading data from files. There are a bunch of concepts you are going to want to learn about to understand this code:
  • Working with structures. When you get a list of image files, their names will be stored inside of a structure.
  • Working with cell arrays. We'll use a cell array to store the data coming from the different images, since it allows for each image to be a different size. If you happen to know that all of your images are exactly the same size you can use a regular numeric array instead.
% Get list of all BMP files in this directory
% DIR returns as a structure array. You will need to use () and . to get
% the file names.
imagefiles = dir('*.bmp');
nfiles = length(imagefiles); % Number of files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
currentimage = imread(currentfilename);
images{ii} = currentimage;
end
Best of luck! - scott

10 Comments

thank you very much for the quick reply,, i will try these code with my work
Thanks a lot.. It was really useful info for me too..
thanx it worked but madam what i have to do if i want to fed those all images to a folder one by one ?? and repeat execution of thefunction until and unless each and every images executes within the function??
Awsome!!!
OMG THANK YOU SO MUCH!!
Its worked like charm, thanks!
Thank you so much!! Does anyone know where I could read up more about cell arrays and structures?
Worked for me as well. Thank you!
Thanks a lot

Sign in to comment.

More Answers (5)

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

thank you very much,, i really appreciated,,
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
yeaayy.. thanks, good idea.
Thank you! This is the exact answer I want!

Sign in to comment.

George
George on 24 Sep 2016
You can try ImageDatastore

3 Comments

but it only works in 2015b and above. is there any alternative to ImageDatastore?
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.
Before R2015a, the closest equivalent was the dataset array from the Statistics toolbox.

Sign in to comment.

DGM
DGM on 24 Jun 2023
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
Anushrita on 16 Jul 2025
% 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

Products

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!