Saveas/print of figure changes 'position' option of current figure handle

4 views (last 30 days)
Hi everybody,
I am currently struggling with the export of figures as the attribute 'position' of the current figure handle seems to be modified by the functions saveas and print. The following minimal example of my code reproduces the malfunction with my installation of the R2017a release:
xData = [0.0 0.5 0.5;1 1 1;1.5 1.5 1.5;2 2 2;2.5 2.5 2.5;3 3 3];
yData = [150 140 130;70 65 60;40 35 30;30 25 20;20 25 20;10 5 0];
figpos1 = zeros(3,4);
figpos2 = zeros(3,4);
for i=1:3
figure
set(gcf,'Units','centimeters','Position',[20 15 4 3],'PaperUnits','centimeters','PaperPositionMode','auto');
for j=1:size(yData,2)
plot(xData(:,i),yData(:,i))
hold on
end
hold off
figpos1(i,:) = get(gcf,'Position');
saveas(gcf,num2str(i),'eps');
figpos2(i,:) = get(gcf,'Position');
end
The variables figpos1 and figpos2 were simply included to compare the values of the attribute 'position' before and after the export. While figpos1 perfectly reproduces the specified position vector, figpos2 randomly exhibits slight changes for some figures. As the figures are supposed to be quite small, slight changes have a strong impact on the appearance of the exported figure.
Has anybody else experienced this issue or has any suggestions on that? May it be an issue of my systems/graphics configuration?
Any help is very much appreciated.
Gerrit

Accepted Answer

Walter Roberson
Walter Roberson on 6 Nov 2019
The way that saveas() and print() work for postscript and pdf is that they resize the figure according to the figure PaperSize and PaperPosition, and save the result. and then resize back. It is therefore normal that small changes could occur unless the PaperSize and PaperPosition combination work out exactly the same as the current display. https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-PaperSize
In theory you can use ResizeFcn callbacks to fine-tune layouts while this happens.
In practice, there were some releases where OpenGL graph appearance at the right hand side could change notably with quite small changes in figure size; if I recall correctly that was at some point before the release you are using, though. (The work-around back in those days was to switch to Painters renderer.)
Historically, there have been some difficulties with getting the right font size and text position on Linux, especially for PDF; your release R2017a might possibly have been one of those releases.
It is generally easiest to ensure that you use a figure InnerPosition property exactly equal to the PaperSize, and ensure that PaperPosition covers the entire virtual paper. Otherwise it becomes a nuisance to adjust text properly.
  1 Comment
Gerrit Heilmann
Gerrit Heilmann on 7 Nov 2019
Wow, thank you so much for that quick and detailed answer. It might be an issue of the 2017a release indeed. The issue did not occur anymore using the 2019b release without modifying my code and thus without explicitly specifying PaperPosition, InnerPosition or other parameters.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!