Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The issue of reading multiple images from a folder in Matlab

Tags:

windows

matlab

I have a set of images located in a folder and I'm trying to read these images and store their names in text file. Where the order of images is very important.

My code as follow:

imagefiles = dir('*jpg');
nfiles = length(imagefiles);    % Number of files found
%*******************
for ii=1:nfiles
    currentfilename = imagefiles(ii).name;
    % write the name in txt file
end

The images stored in the folder in the following sequence : {1,2,3,4,100,110}.

The problem that Matlab read and write the sequence of images as { 1,100,110,2,3,4}. Which is not the correct order.

How can this be overcome?

like image 814
Omar14 Avatar asked Jan 21 '26 13:01

Omar14


1 Answers

I would suggest to use scanf to find the number of the file. For that you have to create a format spec which shows how your file name is built. If it is a number, followed by .jpg, that would be: '%d.jpg'. You can call sscanf (scan string) on the name's of the files using cellfun:

imagefiles = dir('*jpg');
fileNo = cellfun(@(x)sscanf(x,'%d.jpg'),{imagefiles(:).name});

Then you sort fileNo, save the indexes of the sorted array and go through these indexes in the for-loop:

[~,ind] = sort(fileNo);
for ii=ind
    currentfilename = imagefiles(ii).name;
    % write the name in txt file
end
like image 141
hbaderts Avatar answered Jan 24 '26 09:01

hbaderts