MATLAB: Saving snapshots from MATLAB with an extension .png

for loops matlab

I am taking 30 snapshots with a camera connected to my computer. I have developed a for loop that saves the images in the current folder. Here are the codes
photo = videoinput('winvideo', 1);
for i = 1:30
my_image = getsnapshot(photo);
file_name = ['Image' num2str(i)];
imwrite(my_image,file_name,'png');
pause(2);
imshow(my_image(i));
end
The expected output should be
Image1.png
Image2.png
…………………………
Image30.png
In lieu to the above I get
Image1
Image2
…………………………
Image30
Any help please

Best Answer

file_name = sprintf('Image%d.png', i);
Note that when you do
imwrite(my_image,file_name,'png');
then this is not the same as
imwrite(my_image, [file_name,'.png']);
Passing 'png' as the third parameter tells imwrite() to ignore any file extension hint as to desired image format and to write as png content instead, but respecting the passed file name (that is, it is used as-passed, with no extension added on.)