reading all documents with a for loop

7 ビュー (過去 30 日間)
Omer Hakan
Omer Hakan 2022 年 10 月 19 日
編集済み: Image Analyst 2022 年 10 月 19 日
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.

採用された回答

David Hill
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

その他の回答 (1 件)

Image Analyst
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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by