| Printing and Exporting Figures with MATLAB | ![]() |
Using the getframe Command to Export a Graphic
You can use the getframe command 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 command 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. colormap is empty on true color displays. The following example captures the current figure and exports it to a PNG file.
This example exports the current figure to a JPEG file with a quality setting of 50.
You should use the proper syntax of imwrite for the type of image captured. In the examples above, the images are 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- 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 reference page for imwrite. For information about creating a movie from a series of frames, see the reference pages for getframe and movie, or see Movies.
Saving Multiple Figures to an AVI File
You can also save multiple figures to an AVI file using MATLAB's 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 Graphs in AVI Format in "Working Environment for MATLAB."
| Exporting to the Clipboard Using MATLAB Commands | Importing MATLAB Graphics into Other Applications | ![]() |