Using the Print Command (Generating Output) (MATLAB)

Certainly the most powerful way to print your Figure Window, either to a printer or to an image file, is by using the print function. In its simplest use, print will send the contents of the current Figure Window to the computer’s default printer. The general form of print is

PRINT -DEVICETYPE -OPTIONS

Since both the devicetype and options are optional, if you do not supply them, MATLAB will determine the default values for your system. The printer command and device type that print uses by default can be determined by typing

[printcommand,devicetype] = printopt

The function printopt will return the current printer type and output destination. It can also be edited to change your default printer type and destination.

Creating Hardcopy with PRINT

Every time you issue the print command, it determines the default printer settings by issuing a call to the printopt function. The default device type on Windows systems (-dwin) is to print through the Windows Print Manager. The default type is a Level 2 black and white PostScript (-dps2) for Unix and Macintosh systems. As we mentioned in the last section, you can alter the device type and print command by editing the printopt.m file, but we suggest that you consult the Full Product Family Help or the MATLAB Reference Guide for more information about printopt.m before you do. Typing help print in the Command Window will list all the print options, device drivers, etc., available in MATLAB.


Creating Graphics Files Using Print

Essentially, the only thing you need to do to generate electronic copies of your figure is to add a file name to the print statement you used when generating paper copies. The full synopsis for the print command when generating files is

print -devicetype -options filename

For example, the following code segment will generate a file called graphics1.eps in level 1 black and white encapsulated postscript form.

print -deps graphicsl.epsf

If you specify that MATLAB generate an encapsulated PostScript file, it will not be sent to the printer and MATLAB will generate a file. If you do not give it a file name, MATLAB will warn you that it cannot print encapsulated postscript to a printer and will create a file in the current working directory.

Adding Additional Figures to a File

To keep the number of electronic files to a minimum, you may find it useful to append figures to the same file. Normally, if you type

print -deps filename

and the file filename already exists, it will be overwritten without warning. However, by adding on an argument as follows

print -deps -append filename

the figure is added to the end of the file as a new page. Later, the entire set of figures can be conveniently viewed or printed at once.

Publishing Using 4-Color Separation

When generating PostScript and Encapsulated PostScript files, you have the option of generating figures using cyan, magenta, yellow, and black (CMYK) color values instead of red, green, and blue (RGB). This is often needed when sending files to particular color printers that can take advantage of matching figure colors more precisely with CMYK color values. As an example, you might type print -depsc -cmyk filename to generate an Encapsulated color PostScript file.

EPS with a Preview Image

If you are creating encapsulated postscript images to be included in a document, it is useful to have a preview image instead of the postscript message box when you include a figure in your word processor. This makes it easier to remember what the figure contains when you are editing a document. Most modern word processors allow the use of encapsulated postscript with a preview image. If you have a Figure Window open and you type,

print -depsc -tiff -r300 figurel

MATLAB will save the entire contents of the Figure Window at a resolution of 300 dpi in the file figure1.eps and generate a TIFF bit map of the entire Figure Window, always at 72 dpi, that can be used by other applications as the preview image.

Rendering Method with -zbuffer or -painters

There are many methods by which an image can be rendered, i.e., interpreted for printing. When using the print function, by default MATLAB will determine which rendering method to use when printing a figure, however, there are some situations in which you will want to have control over which method gets used. In general, if you are plotting lines and less complex figures, it will be to your advantage to use the painters algorithim (painters). If your plots are more complex, say you are plotting surfaces or using lights, it will be better to use the Z-buffer method (-zbuffer). OpenGL is another rendering method that is available on many computer systems. This method is generally faster than painters or zbuffer and in some cases enables MATLAB to access special graphics hardware that is available on some systems.

If you want control over the printing resolution of a figure use the -zbuffer option and specify the resolution with the print command’s -rnumber option (where number represents the number of dots per inch, dpi). If you are only going to display the figure on your screen or plan on using it in a web page, changing the resolution to a number higher than the default will not provide you with better image quality, but it will increase your file size. The same can be said when sending the file to a printer; only choose a number that is no greater than what your printer can support. Since the Z-buffer method uses raster graphics to draw the figure, the size of the file and memory needed to print will depend on 3 factors: the resolution, the size of the graphic, and whether or not you are using a color or grayscale driver. The OpenGL method can take advantage of compatible video hardware, if it is available, and significantly reduce the time to render an image. Although OpenGL has the potential of being very fast, there are some caveats to consider: 1) OpenGL does not do color map interpolation which means that plots created using surface that use index color with interpolated faces or edges will not be as you expect; 2) Similarly, the phong value for face lighting and edge lighting of surfaces is not supported.

Indicating Which Figure Window to Print

When you have multiple Figure Windows open, you can specify the Figure Window number that you want to print from the command line using the form,

print -fhandle

where handle is the figure number. By default, MATLAB will print the current figure (usually the last one created or the last one that you clicked your mouse in). Later you will be able to appreciate the ability to specify the figure to be printed. Soon you will learn how to create GUIs in MATLAB. In a GUI, for instance, you could provide a user with a GUI window that lets the user create figures in a different window. You might want to have a print button in your GUI, then the callback, which you will learn about later, of that button would need to use the form of the print function in which you pass the figure’s handle so that you can be sure that the figure you intended to print gets printed.

Saving Figures for Future Use

You will notice that from the Figure Window, you can select File ^ Save. If you do so, MATLAB will give you the option to save your Figure Window as a FIG-file with a fig extension to the file name. FIG-files are binary files that contain a complete description of the Figure Window and all that is in it. In previous versions of MATLAB (prior to version 6), figures were saved by saving an M-File and corresponding MAT-File. The FIG-file format did not exist in earlier versions of MATLAB. In those earlier versions, figures were saved as an M-file that generated the figure from data that was stored in a MAT-file. You ran the M-file and it generated your figure. The print command still supports this older approach. The option dmfile tells the print command to create the M-file and if need be the MAT-file to reproduce the figure. Please be aware however, that you will not find reference to it in the helps for print.

print -dmfile filename

To regenerate the figure, just execute the M-file by typing the name of the file you saved.

MATLAB version 6 was a radical departure in many ways from earlier MATLAB versions. There were changes in appearance to some extent, but many of the changes occurred "under the skin" of MATLAB and the FIG-file format was created to accommodate those changes that the earlier figure saving method could not. For example, the earlier method could not keep figure annotations. With the FIG-file, annotations and other changes you make with the plot editing tools are saved.

Next post:

Previous post: