File: ef230-2023-08/www/examples/save_as_png.m Download
function save_as_png(filename)
% Save current figure as a png file named filename.png
% This routine works around MATLAB problems with filled areas
% by saving to a bmp file first, and then converting to a png
% usage: save_as_png('myfile')

%create the new file name with .png appended
newfilename = [filename '.png']; 

% disable the conversion of the background
set(gcf,'inverthardcopy','off'); 

% save it to a temporary bmp file
% bmps use Ghostscript which works better for filled objects
print('-dbmp16m','tmp.bmp','-r300','-noui'); 

% read the temporary bmp file in
img = imread('tmp.bmp'); 

% delete the temporary file
delete('tmp.bmp'); 

% save the image with the specified filename
imwrite(img,newfilename); figure

end % end of save_as_png