Importing multiple image files (DSC0###) in a loop?

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

답변 (1개)

Geoff Hayes
Geoff Hayes 2018년 5월 9일
편집: Geoff Hayes 2018년 5월 9일

0 개 추천

Tessa - you could try using sprintf to create your file names
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

Tessa Klein
Tessa Klein 2018년 5월 9일
편집: Tessa Klein 2018년 5월 9일
Thanks Geoff.. I've updated my code with sprintf but I get the error that my filename does not exist.
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
Jan
Jan 2018년 5월 9일
편집: Jan 2018년 5월 9일
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.

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

카테고리

도움말 센터File Exchange에서 Display Image에 대해 자세히 알아보기

질문:

2018년 5월 9일

편집:

Jan
2018년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by