MATLAB: Print to File

If you are interested in making informative figures that convey a lot of information, generally you need these figures to have high resolution. Low resolution figures can cause lines to be blurry, which can leave data open to interpretation. If you have been following along on this blog with our Figure Sizing, then you know why is it easier to make your figures correctly sized in the beginning. Once your figures are sized (and data plotted, annotated, etc.) you need to save the figures file with sufficient resolution. The defaults in MATLAB (and many other softwares) are not sufficient. In MATLAB, this is easiest to do using the ‘print’ command.

The print command allows you to specify the output file time, along with a number of other parameters. Chief among those parameters is the output resolution. Other options include cropping to the paper size and printing the user controls in the figure box. Each of these parameters can be set to consistently print high resolution figures to files in different file formats. Before getting into the MATLAB script, I will first review different file formats and graphic types.


Bitmap versus Vector Graphics

Both bitmap and vector graphics have their specific uses. Bitmap-type images (*.bmp, *.jpg, *.png, *.gif) are more the more common of the two. In bitmap images, the boundaries between different colors/features are defined by the number of pixels. If you zoom in on a bitmap image, you can see that diagonal lines actually look like mini ‘staircases’ under close inspection. This leads to two key points to consider: 1) the image resolution (in DPI – dots per inch) will set the size of the ‘staircase’ and 2) figures that are resized are done so using interpolation algorithms. This is ultmately why it is critical to create figures that are sized appropriately the first time. Because most productivity software (Word, Powerpoint) use only bitmap type images, if the figure must be resized, there is a potential for the figure to be pixelated, grainy, and low quality. However, if you have sized your figure appropriately when making your figure, you will not have to worry about your figure being pixelated. Software such as MS Paint, Adobe Photoshop, and MS Powerpoint can all be used to make bitmap images but cannot make vector graphics.

Vector graphics, conversely, define their images as a series of functions. Rather than populating individual pixels with different color parameters to produce, say, a black line with a white background, vector graphics store the equation used to make that line. This means that the figure can be resized without losing resolution. Since the equation is known and saved, it can be rescaled to make a bigger (or smaller) figure without pixelation. Typical vector graphics formats are encapsulated postscript (*.eps), postscript (*.ps), and scalable vector graphics (*.svg). Software such as MS Visio, Adobe Illustrator, and Inkscape (my fave!) can be used to make vector graphics (and also bitmaps). On PCs, it is less common to use vector graphics for normal presentations, papers, and proposals unless you are using LaTeX. Instead, a vector graphic can be generated and then converted to bitmap with an arbitrary size. Then it can be readily inserted into Word/Powerpoint. Macs, which I am not familar with, I think have ability to input vector graphics directly into Word/Powerpoint.


File Formats

There are numerous file formats you can use. Some journals specify that they only accept *.tiff or *.eps figures. Also, many specify the resolution of the figures must be >300 DPI. If you are subjected to those constraints, then I suggest you modify the code below to output *.tiff files. I have included it in the script but it is commented by default. As for the specific files you should use, that depends on the software and system you are using. I usually print figures to two separate file formats, one bitmap and one vector. For the vector format, I use a *.eps with a TIFF file preview. The TIFF file preview allows you import *.eps figures into MS Word and Powerpoint. This is a low resolution (72 DPI) file that is meant to be a placeholder. However, if you use a PostScript printer such as CutePDF Writer to print the file to a PDF file, then it will be printed with high resolution (even higher than importing a high resolution bitmap file).

As for the bitmap file, I prefer to use a Portable Network Graphics format (*.png). Of the main files, I find this file format to produce a high quality image without having a large file size. This can be important when you are working in Word/Powerpoint with many figures. If the figures have larger file sizes, then it can cause lag and ultimately crash MS Office. As for the resolution, I typically use 600 DPI as my default. Most journal publications only require 300 DPI resolution. Additionally, most displays do not have a 300 DPI resolution. However, I use double the needed resolution because it does help when the figure must be resized and you cannot (do not have time) to remake the figure. If you are resizing by 5-10%, then the overall quality of the figure will not suffer.


MATLAB Printing Script

Sometimes I use LaTeX and sometimes I use MS Office. As a safe bet, I have established a printing script in MATLAB that will print the figure to both a *.png and a *.eps with a TIFF preview. The script will print the current figure to both *.png and *.eps file formats. Thus, I will have a matched figure in both formats when needed. Additionally, this script allows you to vary the resolution (in DPI) of the figure, and (of course) assumes that the paper size for the figure matches the figure size. In this script, the ‘-loose’ parameter is used to ensure the bounding box is cropped to the edge of the figure, while ‘-noui’ removes the user interface during printing. The link to the file is here, which must be relabeled to a *.m file rather than a *.txt file.

function PrintRoutine( DPI )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function ‘PrintRoutine’ is used to print the current figure file to both
% a high resolution bitmap-based file in Portable Network Graphic (*.png)
% format and a vector-based file in Encapuslated Postscript (*.eps) format.
% Typically, bitmap files are used for WYSIWYG software for word processing
% and presentations (i.e., Microsoft Word and Powerpoint). Vector files are
% commonly used with LaTeX processing. This script prints the current
% figure to both a *.png and a *.eps file whose name is specified when the
% function is called. The output file is sized according to the paper size
% of the figure box and its resolution is either 600 DPI (default) or specified
% via the single input.
%
% Function Inputs
% DPI: desired figure resolution in ‘dots per inch’
%
% Defaults
% If unspecified, the output resolution of the files are set to 600
% DPI
%
% This file is intended to be used with the ‘FigureSetup’ function.
% FigureSetup specifies the size of a figure box in MATLAB and is used to
% scale figures such that they can be imported into files without resizing.
% This ensures the scaling of the figure remains constant. If this function
% is used, it automatically resizes both the figure box and the paper to
% match the desired input size. Thus, figures printed using this file do
% not need to be cropped (for *.png files) or do not need external bounding
% boxes added (for *.eps files).
%
% Written by: Jonathan D. Ellis, PhD
% Last Updated: August 28, 2015
% Version: 1.00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

% Establish the number of input arguments
M = nargin;

 

% Switch to 600 DPI default output resolution if no input is specified
switch M
case {0}
DPI = 600;
end

 

% Verifies whether you want to generate prints for the current figure. This
% is useful and needed for embedding this equation is nested functions
% where multiple figures might be printed.
%
% Answer with either ‘y’ or ‘n’
arg2 = input(‘Do you wish to print PNG & EPS file? (y/n) ‘,’s’);
arg2 = lower(arg2);

 

% If ‘y’ is selected, then the figure will be printed to file.
switch arg2
case {‘y’}
% Input the file name. No extension is needed in the file name as
% it is appended by the ‘print’ command.
print_file = input( ‘Type in File Name: ‘,’s’);
print_file_png = [print_file,’.png’];
print_file_eps = [print_file,’.eps’];
print_file_tiff = [print_file,’.tiff’];
print(print_file_png,[‘-r’,num2str(DPI)],’-dpng’,’-loose’,’-noui’)
print(print_file_eps,[‘-r’,num2str(DPI)],’-depsc’,’-tiff’,’-loose’,’-noui’)
% print(print_file_tiff,[‘-r’,num2str(DPI)],’-dtiff’,’-loose’,’-noui’)
case {n}
display(‘ ‘)
display(‘Figure Not Printed’)
display(‘ ‘)
otherwise
display(‘Selected a wrong option. Figure Not Printed’)
display(‘ ‘)
end

 

end


Script Usage

First you need to have some data to plot, so let us generate that data.

>> t=linspace(0,10,1000)’;
>> A = cos(2*pi*1*t);
>> plot( t , A );

Once the figure is on screen, you can print

>> PrintRoutine

Where you will be prompted to answer two questions

Do you wish to print PNG & EPS file? (y/n) y
Type in File Name: Print_noresize

You should have both png and eps files of the figure. See below for an example of the the png figure. Once again, WordPress doesn’t like eps files. Sorry.

Print_noresize

If you want to ensure the figure is sized correctly, use the FigureSetup (file) first

>> t=linspace(0,10,1000)’;
>> A = cos(2*pi*1*t);
>> plot( t , A );
>> FigureSetup(1,8.3,4)
>> PrintRoutine(1000)
Do you wish to print PNG & EPS file? (y/n) y
Type in File Name: Print_resized

Now you should have figures that are 8.3 cm wide by 8 cm tall that are at 1000 DPI resolution. Below is the png that it should produce.

Print_resized

We still need to add axis labels and change the fonts. However, now we can generate a figure with the right size and print it different file formats for both Productivity software and LaTeX.