saveas - Save figure to specific file format - MATLAB (original) (raw)

Save figure to specific file format

Syntax

Description

saveas([fig](#buou9ii-fig),[filename](#buou9ii-filename)) saves the figure or Simulink® block diagram specified by fig to filefilename. 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, thensaveas saves the figure to a FIG file. To save the current figure, specify fig as gcf.

example

saveas([fig](#buou9ii-fig),[filename](#buou9ii-filename),[formattype](#buou9ii-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.

Extension Resulting 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:

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

Option Format Default 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 generates vector graphics files that scale well when resized. For some complex figures, the files might contain embedded images instead. These images don't scale well, and the extent to which you can edit them in other applications is limited.

To export a figure containing all vector graphics content, use theexportgraphics function, and specify theContentType name-value argument as"vector".

Vector Graphics Formats

Option Format Default 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 andbottom elements of thePaperPosition property. Other formats ignore these values.

Tips

Alternative Functionality

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:

Version History

Introduced before R2006a

expand all

The saveas function no longer clips the content of a large figure when the page is smaller than the figure. Instead, it shrinks the content to fit on the page and preserves the aspect ratio of the figure.

The Renderer property of a figure no longer has no effect on the saved graphic.

The InvertHardCopy property of a figure has no effect. Now, the default background color corresponds to the theme of the figure. The background is white if the figure has the light theme. The background is dark gray (almost black) if the figure has the dark theme. If you set the Color property of the figure before calling saveas, the output uses the color you specify regardless of the theme.

The saveas function no longer supports saving UI components in figures.

To export a figure containing UI components, call the exportapp function. For example, create a simple app containing two buttons and a slider. Export the contents of the figure as a PDF file by calling theexportapp function.

% Create figure with three UI components f = uifigure; button1 = uibutton(f,Position=[150 300 100 50]); button2 = uibutton(f,Position=[300 300 100 50]); slider1 = uislider(f,Position=[150 250 250 3]);

% Export the contents of the figure exportapp(f,"myapp.pdf")

Alternatively, call the getframe function to capture the contents of the figure. Then call the imwrite function to save the content. This time, save the content as a JPEG file.

F = getframe(f); imwrite(F.cdata,"myapp.jpg");

This change was announced in R2022b. In R2023b through R2024b, thesaveas function issues a warning if you export a figure containing UI components.

The saveas function no longer supports creating full-page PostScript (.ps) files. To export vector graphics files, use one of the following methods:

Call the exportgraphics function. Specify an .svg,.eps, .pdf, or .emf file extension and set the ContentType name-value argument to"vector". This function captures content that is tightly cropped around plots, and it does not create full-page output. For example, create a plot and save the contents of the current figure as a PDF file containing vector graphics.

plot([0 3 2 4 1]); exportgraphics(gcf,"myplot.pdf",ContentType="vector")

Alternatively, call the saveas function and specify a.pdf, .eps, .emf, or.svg file extension. For example, create a plot and save the contents of the current figure as an EPS file.

plot([0 3 2 4 1]); saveas(gcf,"myplot.eps","epsc")

Before R2025a: See saveas (R2022a) for details about the PostScript options supported in earlier releases.

This change was announced in R2022b. In R2023b through R2024b, thesaveas function issues a warning when you export a figure to a PostScript file.

The BMP, HDF, PBM, PCX, PGM, and PPM file formats are no longer supported.

To export graphics using one of these formats, use the imwrite function instead. For example, create a line plot and capture the contents of the current figure using thegetframe function. Then save the content as a BMP file.

plot([0 3 2 4 1]); F = getframe(gcf); imwrite(F.cdata,"myplot.bmp");

Before R2025a: See saveas (R2022a) for the file format options supported in earlier releases.

This change was announced in R2022b. In R2023b through R2024b, thesaveas function issues a warning if you export a figure to one of these file formats.

Saved figures match the size of the figure on the screen by default. Previously, saved figures were 8-by-6 inches by default.