MATLAB: How to save filenames

imagesaving

Hello friends,
I want to save files with dynamic name so I wrote the code:
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
imshow(~n1);
imwrite(~n1, n, 'png');
But the file is not saved, is there any mistake in the code? Please guide.

Best Answer

  • Your problem is you didn't look in the help to see what the proper arguments are for imwrite
    imwrite(~n1, n, 'png');
    is not correct. You need to come up with a filename, such as
    baseFileName = sprintf('%d.png', n);
    fullFileName = fullfile(yourFolder, baseFileName); % yourFolder can be pwd if you want.
    imwrite(n1, fullFileName);
    The format argument 'png' is not needed because it determines the format from the filename.
  • Related Question