Error using imwrite with variable filename in for loop

2 ビュー (過去 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']);
  1 件のコメント
Zane Peterkovic
Zane Peterkovic 2021 年 5 月 3 日
Works perfectly, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by