how to read series of images from folder in specific order?

조회 수: 3 (최근 30일)
sana3 sal
sana3 sal 2018년 7월 17일
답변: Diwakar Ravichandran 2018년 7월 18일
Hello there, I am trying to read jpg files from folder with a series of A1,A2,....A22 but when i read them with the following code they read A1,A10,A11,A12...etc then A2,A20,A21 .. etc alphabetically! how i can read them in my specific order?
myFolder = 'C:\Users\JUST\Downloads\Sana3';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
%imshow(imageArray); % Display image.
f=(imageArray);
end
  댓글 수: 2
dpb
dpb 2018년 7월 17일
I'm virtually positive there's a sort routine on FEX for you...I forget whose submittal it was; I think maybe John D'Errico but won't swear it.
It illustrates why when you create such sequential names, alway use a naming pattern that will sort numerically as well as lexically -- like
>> num2str([1:3:22].','File%03d.jpg')
ans =
8×11 char array
'File001.jpg'
'File004.jpg'
'File007.jpg'
'File010.jpg'
'File013.jpg'
'File016.jpg'
'File019.jpg'
'File022.jpg'
>>
for example, and you wouldn't be having the problem.
sana3 sal
sana3 sal 2018년 7월 17일
Thank youuuu, it's worked with me :D

댓글을 달려면 로그인하십시오.

답변 (1개)

Diwakar Ravichandran
Diwakar Ravichandran 2018년 7월 18일
As the solution is already given by @dpb please close this question so that it is off the unanswered questions list. Thank you
Cheers!

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by