Save figure as seen on screen?

I would like to save a figure to png format exactly the same as seen on the screen. I searched the web and found:
set(gcf,'PaperPositionMode','auto'); saveas(gcf,[Filename '.png']);
that supposed to save the figure the same size, but it actually changed the size from 1200x600 to 1875x938, huh?
I also tried setting ‘PaperPosition’ and got the same size 1200x600, but the fonts in the saved figure became much larger compared to on screen and covered up parts of the graph.
If I use the "save" button in the figure window then it saves the figure as seen on the screen without a problem, but I would like to be able to do it programmatically if possible.
Please help. Thanks!

답변 (2개)

Jan
Jan 2013년 1월 25일

12 개 추천

img = getframe(gcf);
imwrite(img.cdata, [FileName, '.png']);

댓글 수: 5

James Ryan
James Ryan 2016년 9월 17일
Perfect.
It's really odd that none of the Save as or Export options on the figure window seem to support this.
Amir Mazouchi
Amir Mazouchi 2017년 10월 30일
use gca instead of gcf to get rid of all margins.
Jan (or anyone who knows the answer), is there a way to "save figure as seen on screen" in .pdf or .eps format?
When I try to do something like
img = getframe(gcf);
imwrite(img.cdata, [FileName, '.eps']);
I get the error "Unable to determine the file format from the file name."
Jan
Jan 2021년 10월 2일
imwrite cannot write eps files. The EPS format is thought for vector graphics, which can be scaled freely. Therefore it is not suitable for saving a figure "as seen on the screen". A raster image like TIFF, PNG or GIF would be matching.
If you use print() to create an EPS of the screenshot taken by getframe, a JPEG image is embedded in the EPS (or maybe a TIFF?). This indirect approach is less usefull than storing a raster image directly.
Martim Zurita
Martim Zurita 2021년 10월 21일
편집: Martim Zurita 2021년 10월 21일
Thanks for the explanation, Jan!
In the end I managed to save my figure as seen on screen as a .pdf, using the export_fig function and the code
export_fig test.pdf

댓글을 달려면 로그인하십시오.

John Petersen
John Petersen 2013년 1월 25일

1 개 추천

It's the same ratio at least. You may want to look at using export_fig() which you can download from the file exchange. This will preserve the figure size.

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

질문:

C
C
2013년 1월 25일

편집:

2021년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by