savesas does not work as saveas in the menu

5 views (last 30 days)
Jan
Jan on 28 Aug 2013
Commented: hamed on 4 Dec 2020
Hi all,
I do not find a solution to the following problem. I want to save a figure automatically from the code. I tried saveas and print as well. I noticed that the saveas command does not work as the saveas which I can select from the figure menu. I run the following code to save my figure:
fileName='test';
h=figure;
%...other plotting code
set(h,'units','normalized','outerposition',[0 0 1 1])
drawnow;
saveas(h,[fileName '.png']);
saveas(h,[fileName '.fig']);
The point is. If I run saveas from the menu and select png. The figure is stored with full screen resolution. If I run the code it is not stored with full screen resolution although the figure is automatically maximized by the code. Using other ways of maximizing the figure did not change the resolution of the png image e.g. set(gcf, 'Position', get(0,'Screensize')).
print('-dpng','-r300','test') will only increase the rate the screen is sampled but not the size of the figure.
The most weird thing is, that saveas does not behave similar to the save as in the menu, I get different resolutions using saveas and the saveas from the menu although I do no change on the figure.
best regards Jan
  1 Comment
Jeff E
Jeff E on 24 Aug 2017
Try adding the line below. It fixed my size discrepancy between "saveas" and using the menu.
h.PaperPositionMode='auto';

Sign in to comment.

Answers (3)

Iain
Iain on 28 Aug 2013
Try using gcf with saveas, not gca.

Jan
Jan on 28 Aug 2013
I tried h,gcf,gca, nothing changed the size of the saved figure.

Peter
Peter on 1 Aug 2014
I had a similar problem. Below is the answer I got that fixed it.
Answer by Namita Vishnubhotla on 31 Jul 2014 at 13:58 Accepted answer
The following code resolves the figure resizing issue:
>> set(fig, 'PaperPositionMode', 'auto');
where 'fig' is your figure handle. You can also just pass 'gcf'.
The 'PaperPositionMode' property (as published in documentation) "ensures that the printed version is the same size as the onscreen version. With 'PaperPositionMode' set to 'auto' MATLAB does not resize the figure to fit the current value of the 'PaperPosition'."
Refer to the various properties listed in the figure properties and axes properties documentation for additional saving and printing settings that may affect the look of your figure when exporting to printer or file.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!