Error using imwrite with variable filename in for loop

조회 수: 1 (최근 30일)
Zane Peterkovic
Zane Peterkovic 2021년 5월 3일
댓글: Stephen23 2021년 5월 3일
I'm trying to make a fairly simple programme to convert a batch of images to grayscale and save them in my current folder.
Convert = fullfile('225C');
imds = imageDatastore(Convert);
for i = 1:149
img = readimage(imds,i);
img = rgb2gray(img);
imwrite(img,[i,'.png']);
end
However, when I run this code I get the error 'Unable to open file ".png" for writing. You might not have write permission.'
Through other forumposts I was lead to believe this should be the correct syntax for a variable filename. I just want 149 files numbered 1-149. Any help appreciated.
  댓글 수: 1
Stephen23
Stephen23 2021년 5월 3일
Rather than concatenating strings, SPRINTF gives more control and makes the intent clear:
sprintf('%d.png',i)

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

채택된 답변

KSSV
KSSV 2021년 5월 3일
Replace the line:
imwrite(img,[i,'.png']);
with
imwrite(img,[num2str(i),'.png']);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by