save plot in an image in matlab

After creating a plot in Matlab to save it as an image you can use the saveas function as follows:

main.m
figure;  % create a new figure (optional)
plot(x,y);  % plot your data
saveas(gcf, 'myplot.png');  % save the plot as a PNG file
129 chars
4 lines

gcf stands for the "current figure" and is used to specify which figure to save.

Alternatively, you can also save the plot in other formats such as PDF, EPS, JPEG, etc. by changing the file extension in the second argument of saveas.

main.m
saveas(gcf, 'myplot.pdf');  % save the plot as a PDF file
saveas(gcf, 'myplot.eps');  % save the plot as an EPS file
saveas(gcf, 'myplot.jpg');  % save the plot as a JPEG file
176 chars
4 lines

Note that the resolution of the output image can be adjusted by specifying additional parameters in the saveas function. For instance, to save the plot as a PNG file with a resolution of 300 DPI, you can use the following code:

main.m
saveas(gcf, 'myplot.png', 'png', 'Resolution', 300);
53 chars
2 lines

related categories

gistlibby LogSnag