Main Content

saveas

Save figure to specific file format

Description

saveas(fig,filename) saves the figure or Simulink® block diagram specified by fig to file filename. Specify the file name as a character vector or 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.

example

Examples

collapse all

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')

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 a Simulink block diagram named 'sldemo_tank' as a JPEG file. Use get_param to get the handle of the diagram. You must have Simulink installed to run this code.

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

Input Arguments

collapse all

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.

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'.

openExample('sldemo_tank')
saveas(get_param('sldemo_tank','Handle'),'MySimulinkDiagram.png');

File name, specified as a character vector or 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

.tif

TIFF image, compressed

Data Types: char | string

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 valid 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.

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

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

Image File

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

Image File 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

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, vector graphics might contain stray lines or other visual artifacts. 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. Also, if saveas uses the OpenGL renderer to generate the file, then transparency is not supported. 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

Note

Only the PDF format uses the PaperOrientation property of the figure and the left and bottom elements of the PaperPosition property. Other formats ignore these values.

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 with Specific Size, Resolution, or Background Color.

Alternative Functionality

Starting in R2020a, you can use the exportgraphics function to save the contents of any axes, figure, chart that can be a child of a figure, tiled chart layout, or container such as a panel. This function provides a better alternative to the saveas function when you want to:

  • Save graphics displayed in an app or in MATLAB Online™

  • Minimize the white space around the content

  • Save a PDF fragment with embeddable fonts

  • Save a subset of the content in the figure

  • Control the background color without having to modify properties on the figure

Version History

Introduced before R2006a

expand all