help exportgraphics
EXPORTGRAPHICS Save plot or graphics content to file
EXPORTGRAPHICS(OBJ, FILESPEC) saves the specified graphics to a file.
OBJ is the handle of any type of an axes, a figure, a chart that can
be a child of the figure, a tiled chart layout, or a container within
the figure. FILESPEC is a character vector or string scalar specifying
the name of a file to create. It must include the extension, and can
optionally include a full or relative path. EXPORTGRAPHICS supports
creating JPEG, PNG, and TIFF images, and EPS, PDF, and EMF vector
formats (EMF is only supported on Windows).
EXPORTGRAPHICS(___, OPTIONS) saves the graphics to the file, applying
the specified OPTIONS. OPTIONS is one or more of the following
name-value pairs.
'ContentType', CONTENTTYPE how to generate content in vector format files
CONTENTTYPE can be one of the following values
- 'vector' to generate vectorized (scalable) output
- 'image' to generate rasterized output (embed image)
- 'auto' allow MATLAB heuristic to decide
The default value is 'auto'
'Resolution', DPI specifies output resolution for images. DPI must be
a positive whole number. The default value is 150
'BackgroundColor', COLORSPEC specifies the color to use as the background
COLORSPEC can be one of the following values
- a color name, such as 'red', or 'w'
- an RGB triplet, such as [1 0 0] or [1 1 1]
- a hexadecimal color code, such as '#FFF' or '#FFFFFF')
- 'none' to use either a white or transparent background, depending on
what the output format supports
- 'current' to use the graphic's current background color
The default value is 'white'
'Colorspace', COLORSPACE the color space to use when generating output
COLORSPACE can be one of the following values
- 'rgb' generates RGB true color output
- 'gray' generates grayscale output
- 'cmyk' generates cyan, magenta, yellow, black output
The default value is 'rgb'
'Append', APPEND specifies whether to append the output to an existing file or replace the content.
APPEND can have a value of true or false.
- true appends the output to an existing file
- false replaces the content in an existing file
The default value is false
Example: Export a plot to a pdf file
plot(rand(3));
exportgraphics(gca, 'results.pdf');
Example: Export a plot to a pdf file and ensure output is vectorized/scalable
plot(rand(3));
exportgraphics(gca, 'results.pdf', 'ContentType', 'vector');
Example: Export a plot as an image to a pdf file
plot(rand(3));
exportgraphics(gca, 'results.pdf', 'ContentType', 'image');
Example: Export a plot and append to an existing pdf file
plot(rand(3));
exportgraphics(gca, 'results.pdf', 'Append', true);
Example: Export a specific plot from the figure
fig = figure;
ax1 = subplot(2,1,1); bar(magic(4));
ax2 = subplot(2,1,2); plot(rand(4));
exportgraphics(ax2, 'results.png');
Example: Export all plots from figure to an image file at 300DPI
subplot(2,1,1); bar(magic(4));
subplot(2,1,2); plot(rand(4));
exportgraphics(gcf, 'results.png', 'Resolution', 300);
Example: Export a plot to an image file with a red background
plot(rand(3));
exportgraphics(gca, 'results.png', 'BackgroundColor', [1 0 0]);
Example: Export a plot to a grayscale image file
plot(rand(3));
exportgraphics(gca, 'results.png', 'Colorspace', 'gray');
See also COPYGRAPHICS, EXPORTAPP
Documentation for exportgraphics
doc exportgraphics