MATLAB Answers

How to read images in a folder

2.317 views (last 30 days)
Nilushi Dissanayake
Nilushi Dissanayake on 25 Jan 2011
Commented: vikram punia on 3 Nov 2020 at 15:14
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-02.bmp etc.
I need help for this because I'm a beginner to MATLAB.


Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick 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);
S(k).data = I; % optional, save data.
You might like to take a look at the examples in the documentation:
Lenin Falconi
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
vikram punia on 3 Nov 2020 at 15:14
Thanks, Really helped.

Sign in to comment.

Accepted Answer

Michelle Hirsch
Michelle Hirsch on 25 Jan 2011
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;
Best of luck! - scott


Show 6 older comments
Muhammad  Shahzaib
Muhammad Shahzaib on 26 May 2020
Its worked like charm, thanks!
Haritha GB
Haritha GB on 15 Jul 2020
Thank you so much!! Does anyone know where I could read up more about cell arrays and structures?
Hasaan Ijaz
Hasaan Ijaz on 12 Sep 2020
Worked for me as well. Thank you!

Sign in to comment.

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


Show 1 older comment
Tong Liang
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
Liu Sam on 4 Mar 2018
Thank you! This is the exact answer I want!

Sign in to comment.

Walter Roberson
Walter Roberson on 25 Jan 2011
Please check the FAQ on this topic

  1 Comment

Nilushi Dissanayake
Nilushi Dissanayake on 25 Jan 2011
thank you very much,,

Sign in to comment.

George on 24 Sep 2016
You can try ImageDatastore


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

Sign in to comment.

Paul De
Paul De on 9 Nov 2017
THANK YOu! I've been looking for a long time the solution of this problem)


Sign in to comment.

Ashraf Rayed
Ashraf Rayed on 12 May 2020
okay it is awesoe coding. Thanks a lot. I have a question here, look here we will read some pictures of a folder and we will get some output also of those pictures. Can we save those data sequentially in a xcel sheet??


Sign in to comment.


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!

Translated by