Graphics |
Export a figure in a graphics format to a file if you want to import it into another application, such as a word processor. You can export to a file from the Windows or UNIX Export dialog box or from the command line.
This section tells you how to export your figure to a file
For further information, see Choosing a Graphics Format.
Using the Graphical User Interface
Use the Export dialog box to select a file format, specify a filename, and locate the directory to which you want to save your file. To open the Export dialog box, select Export from the figure window's File menu.
Select the graphics file format you want the exported figure to have using the Save as type list box. For information on the graphics file formats supported by MATLAB, see Choosing a Graphics Format.
Use the print
function to print from the MATLAB command line or from a program. See Printing and Exporting with print for basic information on printing from the command line.
To export the current or most recently active figure, type
where fileformat
is a graphics format supported by MATLAB and filename
is the name you want to give to the export file. MATLAB selects the filename extension, if you don't specify it.
You can also specify a number of options with the print
function. These are shown in the Printing Options table on the print
reference page.
For example, to export Figure No. 2 to file spline2d.eps
, with 600 dpi resolution, and using the EPS color graphics format, type
Graphics file formats are explained in more detail in the sections, Choosing a Graphics Format and Description of Selected Graphics Formats.
Exporting with getframe
You can use the getframe
function with imwrite
to export a graphic. getframe
is often used in a loop to get a series of frames (figures) with the intention of creating a movie.
Some of the benefits of using this export method over using print
are
getframe
to capture a portion of the figure, rather than the whole figure.
imwrite
offers greater flexibility for setting format-specific options, such as the bit depth and compression.
The drawbacks of using this method are that imwrite
uses built-in MATLAB formats only. Therefore, you will not have access to the Ghostscript formats available to you when exporting with the print
function or Export menu. Also, this technique is limited to screen resolution.
How to Use getframe and imwrite. Use getframe
to capture a figure and imwrite
to save it to a file. getframe
returns a structure containing the fields cdata
and colormap
. The colormap
field is empty on true color displays. The following example captures the current figure and exports it to a PNG file.
You should use the proper syntax of imwrite
for the type of image captured. In the example above, the image is captured from a true color display. Since the colormap
field is empty, it is not passed to imwrite
.
Example -- Exporting a Figure Using getframe and imwrite. This example offers device independence -- it will work for either RGB-mode or indexed-mode monitors.
X=getframe(gcf); if isempty(X.colormap) imwrite(X.cdata, 'myplot.bmp') else imwrite(X.cdata, X.colormap, 'myplot.tif') end
For information about available file formats and format-specific options, see the imwrite
reference page. For information about creating a movie from a series of frames, see the reference pages for getframe
and movie
, or see Movies in Creating Specialized Plots.
Saving Multiple Figures to an AVI File
You can also save multiple figures to an AVI file using the MATLAB avifile
and addframe
functions. AVI files can be used for animated sequences and do not need MATLAB to run, but do require an AVI viewer. For more information, see Exporting MATLAB Graphs in AVI Format," in the "Development Environment" section of the Using MATLAB documentation.
Importing MATLAB Graphics into Other Applications
You can include MATLAB graphics in a wide variety of applications for word processing, slide preparation, modification by a graphics program, presentation on the Internet, and so on. In general, the process is the same for all applications:
Edit Before You Export. Vector graphics may be fully editable in a few high-end applications, but most applications do not support editing beyond simple resizing. Bitmaps cannot be edited with quality results unless you use a software package devoted to image processing. In general, you should try to make all the necessary settings while your figure is still in MATLAB.
Importing into Microsoft Applications. To import your exported figure into a Microsoft application, select Picture from the Insert menu. Then select From File and navigate to your exported file. If you use the clipboard to perform your export operations, you can take advantage of the recommended MATLAB settings for Word and PowerPoint.
Example -- Importing an EPS Graphic into LaTeX. This example shows how to import an EPS file named peaks.eps
into LaTeX.
\documentclass{article} \usepackage{graphicx} \begin{document} \begin{figure}[h] \centerline{\includegraphics[height=10cm]{peaks.eps}} \caption{Surface Plot of Peaks} \end{figure} \end{document}
EPS graphics can be edited after being imported to LaTeX. For example, you can specify the height in any LaTeX-compatible dimension. To set the height to 3.5 inches, use the command
You can use the angle
function to rotate the graph. For example, to rotate the graph 90 degrees, add
to the same line of code that sets the height, i.e., [height=10cm,angle=90]
.
Printing to a File | Exporting to the Windows Clipboard |