Reading Multiple Images from Folder

4 views (last 30 days)
Chandra Shekhar
Chandra Shekhar on 25 Jan 2013
Edited: Stephen23 on 18 Apr 2021
I have a folder named 'ImageSet1',it consist of 20 images named as a 1.jpg,2.jpg...20.jpg.
Here i want to read images from 1 to 10 and then from 11 to 20 separately. it means, when i read image 1,immediately i have to read 11th image and when i read image 2,immediately i have to read 12th image in a loop and so on.. Here is the my code
sdirectory = 'ImageSet1';
jpegfiles = dir([sdirectory '/*.jpg']);
for k = 1:length(jpegfiles)/2
filename = [sdirectory '/' jpegfiles(k).name];
I = imread(filename);
filename1=[sdirectory '/' jpegfiles(10+k).name];
This code is not reading in order,like 1 and 11th image,2 and 12th image...
Does any one know please correct this code or any other method..?
Stephen23 on 27 May 2015
Edited: Stephen23 on 18 Apr 2021
A simple solution is to use my FEX submission natsortfiles
>> S = dir('*.txt');
ans =
ans =
ans =
>> S = natsortfiles(S); % alphanumeric sort by filename
ans =
ans =
ans =

Sign in to comment.

Answers (2)

Evgeny Pr
Evgeny Pr on 25 Jan 2013
Edited: Evgeny Pr on 25 Jan 2013
Use natural sorting for image filenames.
Listing = dir(fullfile(directoryPath, '*.jpg'));
names = {}';
names = sort_nat(names);
fullNames = cellfun(@(x) fullfile(directoryPath, x), names, 'UniformOutput', 0);

azizullah khan
azizullah khan on 29 Nov 2013
dear sir kindly explain me names={} what does it means


Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!