reading all documents with a for loop
조회 수: 8 (최근 30일)
이전 댓글 표시
I have 750 pictures that I want to work on my code one by one. Their names are like
PetsD2TeC1_00000.jpg
PetsD2TeC1_00001.jpg
PetsD2TeC1_00002.jpg
...
PetsD2TeC1_00750.jpg
Please keep that in mind, there are zeros depending on how many digits the index number has.
댓글 수: 0
채택된 답변
David Hill
2022년 10월 19일
편집: David Hill
2022년 10월 19일
Assumig all the pictures are the same size.
for k=1:750
A(:,:,:,k)=imread(sprintf('PetsD2TeC1_00%03d.jpg',k));%store all pictures in 4D matrix
end
댓글 수: 0
추가 답변 (1개)
Image Analyst
2022년 10월 19일
편집: Image Analyst
2022년 10월 19일
This gets asked several times per week. See the FAQ for code snippet
Or, more general and able to handle numbers past 999, or whatever the exact number you have is:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, 'PETS*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, 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.
drawnow; % Force display to update immediately.
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!