Importing multiple image files (DSC0###) in a loop?
Afficher commentaires plus anciens
I'm trying to import 10-100 image files with imread to resize with imresize, and save them under a new name with imwrite in MATLAB.
I got the variable picfile to be assigned to the exact file name on my memory card, but am having trouble getting imread to accept the variable's contents
Error using imread>get_full_filename (line 516) File "picfile" does not exist.
I think MATLAB is searching my memory card for something like ' picfile.jpg' when I want it to look for ' DSC0345.jpg'
fprintf('THIS WILL ONLY WORK FOR PICTURE FILE NAMES STARTING WITH DSC0\n')
firstpic=input('Complete the first picture''s filename: DSC0');
picname(1)=firstpic;
dsc='DSC0';
numpics=input('How many sample pictures are in this project? ');
for i=2:1:numpics
picname(i)=firstpic+i;
picfile = sprintf('DSC0%0.0f.jpg',picname(i));
imshow('picfile')
end
Réponses (1)
Geoff Hayes
le 9 Mai 2018
Modifié(e) : Geoff Hayes
le 9 Mai 2018
for k=1:100
filename = sprintf('DSC%03d.png',k);
% now load file
end
Note how we use the %03d so that up to two zeros will be used (preprended to the integer k) when creating the filename.
2 commentaires
Tessa Klein
le 9 Mai 2018
Modifié(e) : Tessa Klein
le 9 Mai 2018
No, Matlab does not search for "picfile.jpg", but you instructed it clearly to search for "DSC0%d.jpg". Either you are in the wrong folder, or the file name does not exist. Solution: 1. use absolute paths and check the existence of the file:
folder = 'E:\'; % Adjust this
for k = 2:numpics
picfile = fullfile(folder, sprintf('DSC0%d.jpg', firstpic + k));
if exist(picfile, 'file')
imshow('picfile')
else
fprintf(2, 'Missing file: %s\n', picfile);
end
end
Maybe the files are called "DSC00003.jpg" etc? Then:
picfile = fullfile(folder, sprintf('DSC%05d.jpg', firstpic + k));
as Geoff has suggested already. This inserts the required number of zeros automatically.
Catégories
En savoir plus sur Display Image dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!