Crowley
Crowley

Reputation: 2331

How to avoid figure cropping in older versions of Matlab

During answering question Matlab figure to pdf: measuring accuracy and learning from the answer Limit of figure dimensions another question has risen.

How to protect the figure to be resized when it is about to exceed the limits?

Upvotes: 0

Views: 33

Answers (1)

Crowley
Crowley

Reputation: 2331

Suppose we are using matlab 2011b with screen resolution 1400 x 900 px and resolution of 96 ppi and we want to export a figure with size of 10" x 20", which exceed the limits for sure.

FigureSize=[10 20];
FigureInchSize=FigureSize.*1;           %\ Convert the given size to inches
ScrSize=get(0,'ScreenSize');
ScrSize=ScrSize(3:4);
PPI_def=get(0,'ScreenPixelsPerInch');
PPI_new=PPI_def;

%\\ Calculate the appropriate resolution PPI_new
if FigureSize(1)*PPI_new>ScrSize(1)    %\ Will the figure width exceed the limit?
  PPI_new=floor(ScrSize(1)/FigureInchSize(1));
end
if FigureSize(2)*PPI_new>ScrSize(2)    % Will the figure height exceed (new) limit?
  PPI_new=floor(ScrSize(2)/FigureInchSize(2));
end

set(0,'ScreenPixelsPerInch',PPI_new);
set(FigureHandle,'position',[0.1,0.1,FigureSize]);
%\\ Export the figure
export_fig('Foo','-pdf','-nocrop');
%\\ Reset the resolution
set(0,'ScreenPixelPerInch',PPI_def);

In first part we read necessary values and convert them to suitable format. We also avoid automatic conversion via set(Handle,'Units',<Units>) which may interfere with interpretation of Position values.

In second part we change the resolution value, if needed.

In the third part we change the resolution, resize and export the figure and return the resolution back to default values.

Be careful when and how you define the layout of the figure.

Upvotes: 0

Related Questions