Skip to content

Instantly share code, notes, and snippets.

@weibeld
Last active March 22, 2023 16:58
Show Gist options
  • Save weibeld/cd00911790f4dfc5062b to your computer and use it in GitHub Desktop.
Save weibeld/cd00911790f4dfc5062b to your computer and use it in GitHub Desktop.
Matlab functions for setting tight margins around a figure, and setting the paper size to the figure size
% setCrop - Adjust paper size to figure size of current figure.
%
% Usage:
% setCrop()
%
% Notes:
% Call this function after all plotting commands, after calling
% "setTightMargins", but before calling "print".
%
% Daniel Weibel <danielmweibel@gmail.com> November 2015
%------------------------------------------------------------------------------%
function setCrop()
% Get current figure handle
fig = gcf;
% Synchronise units for screen and paper
fig.Units = 'centimeters';
fig.PaperUnits = 'centimeters';
% Get width and heigth of figure (in centimeters)
fig_width = fig.Position(3);
fig_height = fig.Position(4);
% Activate auto-positioning of figure on paper
fig.PaperPositionMode = 'auto';
% Set paper size to figure size
fig.PaperSize = [fig_width, fig_height];
% setTightMargins - Remove or set margins of current figure.
%
% Usage:
% setTightMargins(): remove all margins completely.
%
% setTightMagins(mrg): set margins to "mrg".
%
% Input:
% mrg: either a scalar specifying margin (in cm) for all four sides, or
% a vector of size 4 where mrg(1) specifies "left", mrg(2) "bottom",
% mrg(3) "top", and mrg(4) "right" margin.
%
% Note:
% Call this function after all plotting commands and before calling "setCrop"
% and "print".
%
% Daniel Weibel <danielmweibel@gmail.com> November 2015
%------------------------------------------------------------------------------%
function setTightMargins(mrg)
% If the optional argument 'mrg' is specified
if nargin == 1
if length(mrg) == 4 my_mrg = mrg;
elseif length(mrg) == 1 my_mrg(1:4) = mrg;
else error('Argument 1 must have length 1 or 4.'); end
else
my_mrg(1:4) = 0;
end
% Get handles of current figure and axes
fig = gcf; % Current figure handle
ax = gca; % Current axes handle
% Synchronise units of figure an axes object
fig.Units = 'centimeters';
ax.Units = 'centimeters';
% Get minimum margins around plot (as calculated by Matlab)
min_margin_l = ax.TightInset(1);
min_margin_b = ax.TightInset(2);
min_margin_r = ax.TightInset(3);
min_margin_t = ax.TightInset(4);
% Add custom margins from optional argument 'mrg' to the minimum margins
margin_l = min_margin_l + my_mrg(1);
margin_b = min_margin_b + my_mrg(2);
margin_r = min_margin_r + my_mrg(3);
margin_t = min_margin_t + my_mrg(4);
% Get width and height of entire figure (in centimeters)
fig_width = fig.Position(3);
fig_height = fig.Position(4);
% Set dimensions of axes regions to match custom margins
left = margin_l;
bottom = margin_b;
width = fig_width - margin_l - margin_r;
height = fig_height - margin_b - margin_t;
ax.Position = [left, bottom, width, height];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment