Classes - Beard

You are here: start » latex_hints » print_figure_to_pdf



Exporting Matlab Figures to PDF with a Matlab Function

This page provides code for a printFigureToPdf() function that can automatically crop and export a Matlab figure to a pdf (using vector graphics), like this one. That pdf was generated with the following code, which demonstrates how the function can be used:

% Set up sample data
x = -20:.1:20;
y = sin(2*pi*x/20);

% Draw a sample plot
xlabel('x'); ylabel('y');
title('Figure 1');

% Output the plot to a pdf file, and make it 6 inches by 4 inches
printFigureToPdf('fig1.pdf', [6,4],'in');

% Open the pdf for viewing
open fig1.pdf

The code for the printFigureToPdf() function is given below. The function automatically tries to crop away any extra whitespace around the figure and exports it to the exact size you specify. I have seen two problems crop up (both of which are fixable):

  1. In some cases (particularly when using subplot), the function is not able to automatically crop away the extra whitespace. This can be corrected by manually cropping the figure in LaTeX (see the options for the \includegraphics command).
  2. There are occasional cases when this function will crop a little to much (thus cutting off part of a letter, title, etc). This can be corrected using the extraSpace parameter, as explained in the comments in the code.
function printFigureToPdf(fileName, figureSize, units, extraSpace, figHandle)
	% printFigureToPdf(fileName, figureSize, units, figHandle)
	% Outputs a pdf of the figure where all the extra whitespace has been
	% cropped out.  The paper size in the pdf will match the size of the
	% figure as well.  Note that having a suptitle in the figure seems to
	% mess up the part of this function that sets tight margins.
	%	fileName - The name of the pdf file to create, e.g. 'fig.pdf'
	%	figureSize - The size of the figure, e.g. [6.5 4]
	%	units - The units used for figureSize, e.g. 'inches'
	%	extraSpace - Optional parameter.  The amount of extra space (in
	%		normalized units) to put on the left, bottom, right, and top
	%		sides of the graph.  Use this if something is getting cut off.
	%		The format is [leftSpace, bottomSpace, rightSpace, topSpace].
	%		So, [.1, .1, .1, .1] would put 10% extra space on all sides.
	%		If [] is passed in, the default [0 0 0 0] will be used.
	%	figHandle - Optional parameter.  The handle to the figure to output
	%		to a pdf.  If omitted, gcf will be used.
	if(nargin < 4 || isempty(extraSpace))
		extraSpace = [0 0 0 0];
	if(nargin < 5)
		figHandle = gcf;
	% Store the old state so it can be restored
	axisHandle = get(figHandle, 'CurrentAxes');
	oldLooseInset = get(axisHandle, 'LooseInset');
	oldPaperUnits = get(figHandle, 'PaperUnits');
	oldPaperSize = get(figHandle, 'PaperSize');
	oldPaperPositionMode = get(figHandle, 'PaperPositionMode');
	oldPaperPosition = get(figHandle, 'PaperPosition');
	oldRenderer = get(figHandle, 'renderer');
	% Eliminate extra whitespace in the figure
	set(axisHandle, 'LooseInset', get(axisHandle, 'TightInset') + extraSpace);

	% Set up the paper size / position
	set(figHandle, 'PaperUnits', units);
	set(figHandle, 'PaperSize', figureSize);    % Set to final desired size here as well as 2 lines below
	set(figHandle, 'PaperPositionMode', 'manual');
	set(figHandle, 'PaperPosition', [0 0 figureSize]);

	% To guarantee vector graphics output (doesn't support transparency though)
	set(figHandle, 'renderer', 'painters');

	% Output the figure
	print(figHandle, '-dpdf', fileName);
	% Restore previous state
	set(axisHandle, 'LooseInset', oldLooseInset);
	set(figHandle, 'PaperUnits', oldPaperUnits);
	set(figHandle, 'PaperSize', oldPaperSize);
	set(figHandle, 'PaperPositionMode', oldPaperPositionMode);
	set(figHandle, 'PaperPosition', oldPaperPosition);
	set(figHandle, 'renderer', oldRenderer);