Documentation

saveas

Save figure to specific file format

Syntax

Description

example

saveas(fig,filename) saves the figure or Simulink® block diagram specified by fig to file filename. Specify the file name as a string that includes a file extension, for example, 'myplot.jpg'. The file extension defines the file format. If you do not specify an extension, then saveas saves the figure to a FIG-file.To save the current figure, specify fig as gcf.

example

saveas(fig,filename,formattype) creates the file using the specified file format, formattype. If you do not specify a file extension in the file name, for example, 'myplot', then the standard extension corresponding to the specified format automatically appends to the file name. If you specify a file extension, it does not have to match the format. saveas uses formattype for the format, but saves the file with the specified extension. Thus, the file extension might not match the actual format used.

Examples

collapse all

Save Figure as PNG File

Create a bar chart and save it as a PNG file.

x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,'Barchart.png')

Save Figure as EPS File

Create a bar chart and save it as an EPS file. Specify the 'epsc' driver to save it in color.

x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,'Barchart','epsc')

saveas saves the bar chart as Barchart.eps.

Save Simulink Block Diagram as BMP File

Save a Simulink block diagram named 'sldemo_tank' as a BMP file. Use get_param to get the handle of the diagram. You must have Simulink installed to run this code.

sldemo_tank
fig = get_param('sldemo_tank','Handle');
saveas(fig,'MySimulinkDiagram.bmp');

Related Examples

Input Arguments

collapse all

fig — Figure to savefigure object | Simulink block diagram

Figure to save, specified as a figure object or a Simulink block diagram. If you specify other types of graphics objects, such as an axes, then saveas saves the parent figure to the object. This means that saveas cannot save a subplot without also saving all subplots in the parent figure.

Example: saveas(gcf,'MyFigure.png')

To save a Simulink block diagram, use get_param to get the handle of the diagram. For example, save a block diagram named 'sldemo_tank'.

sldemo_tank
saveas(get_param('sldemo_tank','Handle'),'MySimulinkDiagram.bmp');

filename — File namestring

File name, specified as a string with or without a file extension.

Example: 'Bar Chart'

Example: 'Bar Chart.png'

If you specify a file extension, then saveas uses the associated format. If you specify a file extension and additionally specify the formattype input argument, then saveas uses formattype for the format and saves the file with the specified file name. Thus, the file extension might not match the actual format used.

You can specify any extension corresponding to a file format. This table lists some common file extensions.

ExtensionResulting Format
.fig

MATLAB® FIG-file (invalid for Simulink block diagrams)

.m

MATLAB FIG-file and MATLAB code that opens figure (invalid for Simulink block diagrams)

.jpg

JPEG image

.png

Portable Network Graphics

.eps

EPS Level 3 Black and White

.pdf

Portable Document Format

.bmp

Windows® bitmap

.emf

Enhanced metafile

.pbm

Portable bitmap

.pcx

Paintbrush 24-bit

.pgm

Portable Graymap

.ppm

Portable Pixmap

.tif

TIFF image, compressed

formattype — File format'fig' | 'm' | 'mfig' | bitmap image file format | vector graphics file format

File format, specified as one of these options:

  • 'fig' — Save the figure as a MATLAB figure file with the .fig extension. To open figures saved with the .fig extension, use the openfig function. This format is not value for Simulink block diagrams.

  • 'm' or 'mfig' — Save the figure as a MATLAB figure file and additionally create a MATLAB file that opens the figure. To open the figure, run the MATLAB file. This option is not valid for Simulink block diagrams.

  • Bitmap image file format — Specify the format as one of the bitmap image options in the table, Bitmap Image Formats.

  • Vector graphics file format — Specify the format as one of the vector graphics options in the table, Vector Graphics Formats.

Bitmap Image File

Bitmap images contain a pixel-based representation of the figure. The size of the generated file depends on the figure and the format used. Bitmap images are widely used by web browsers and other applications that display graphics. However, they do not scale well and you cannot modify individual graphics objects (such as lines and text) in other graphics applications.

Bitmap Image Formats

OptionFormatDefault File Extension
'jpeg'JPEG 24-bit.jpg
'png'PNG 24-bit.png
'tiff'TIFF 24-bit (compressed).tif
'tiffn'TIFF 24-bit (not compressed).tif
'meta'Enhanced metafile (Windows only).emf
'bmpmono'BMP Monochrome .bmp
'bmp'BMP 24-bit .bmp
'bmp16m'BMP 24-bit .bmp
'bmp256'BMP 8-bit (256 color, uses a fixed colormap) .bmp
'hdf'HDF 24-bit.hdf
'pbm'PBM (plain format) 1-bit.pbm
'pbmraw'PBM (raw format) 1-bit.pbm
'pcxmono'PCX 1-bit.pcx
'pcx24b'PCX 24-bit color (three 8-bit planes).pcx
'pcx256'PCX 8-bit newer color (256 color).pcx
'pcx16'PCX older color (EGA/VGA 16-color).pcx
'pgm'PGM (plain format).pgm
'pgmraw'PGM (raw format).pgm
'ppm'PPM (plain format).ppm
'ppmraw'PPM (raw format).ppm

Vector Graphics File

Vector graphics files store commands that redraw the figure. This type of format scales well, but can result in a large file. In some cases, a vector graphics format might not produce the correct 3-D arrangement of objects. Some applications support extensive editing of vector graphics formats, but others do not support editing beyond resizing the graphic. The best practice is to make all the necessary changes while your figure is still in MATLAB.

Typically, saveas uses the Painters renderer when generating vector graphics files. For some complex figures, saveas uses the OpenGL® renderer instead. If it uses the OpenGL renderer, then the vector graphics file contains an embedded image, which might limit the extent to which you can edit the image in other applications. To ensure that saveas uses the Painters renderer, set the Renderer property for the figure to 'painters'.

If you set the Renderer property for the figure, then saveas uses that renderer. Otherwise, it chooses the appropriate renderer. However, if saveas chooses a renderer that differs from the renderer used for the figure on the display, then some details of the saved figure can differ from the displayed figure. If necessary, you can make the displayed figure and the saved figure use the same renderer by setting the Renderer property for the figure.

Vector Graphics Formats

OptionFormatDefault File Extension
'pdf'Full page Portable Document Format (PDF) color.pdf
'eps'Encapsulated PostScript® (EPS) Level 3 black and white.eps
'epsc'Encapsulated PostScript (EPS) Level 3 color.eps
'eps2'Encapsulated PostScript (EPS) Level 2 black and white.eps
'epsc2'Encapsulated PostScript (EPS) Level 2 color.eps
'meta'Enhanced Metafile (Windows only).emf
'svg'SVG (scalable vector graphics).svg
'ps'Full-page PostScript (PS) Level 3 black and white.ps
'psc'Full-page PostScript (PS) Level 3 color.ps
'ps2'Full-page PostScript (PS) Level 2 black and white.ps
'psc2'Full-page PostScript (PS) Level 2 color.ps

    Note:   Only PDF and PS formats use the PaperOrientation property of the figure and the left and bottom elements of the PaperPosition property. Other formats ignore these values.

More About

collapse all

Tips

  • To control the size or resolution when you save a figure, use the print function instead.

  • The saveas function and the Save As dialog box (accessed from the File menu) do not produce identical results. The Save As dialog box produces images at screen resolution and at screen size. The saveas function uses a resolution of 150 DPI and uses the PaperPosition and PaperPositionMode properties of the figure to determine the size of the image.

  • Details of saved and printed figures can differ from the figure on the display. To get output that is more consistent with the display, see Save Figure Preserving Background Color and Save Figure at Specific Size and Resolution.

See Also

| |

Introduced before R2006a

Was this topic helpful?