exportgraphics

Saves a figure, axes or chart to an image or vector file.

Category: Graphics > Printing and Saving

Syntax

exportgraphics(obj, filename)

Writes obj to filename; the format comes from the extension (PNG, JPG, TIFF, PDF, SVG, EPS and others).

exportgraphics(obj, filename, Name, Value)

Adds options such as 'Resolution', 'BackgroundColor' or 'ContentType'.

Description

Compared with saveas, exportgraphics trims surrounding whitespace by default and gives control over resolution and background. Exporting a single axes saves only that plot area, which is handy for figures in reports.

Vector formats (PDF, SVG, EPS) stay sharp at any zoom; raster formats depend on the resolution.

Input arguments

obj
Figure, axes, or chart object to export.
filename
Output file name with extension.
'Resolution'
Dots per inch for raster formats.
'BackgroundColor'
Background color; 'none' gives a transparent background where supported.

Example

Export the current axes

plot(1:5, (1:5).^2)
try
  exportgraphics(gca, 'curve.png', 'Resolution', 150);
  disp('saved curve.png')
catch
  disp('needs the editor renderer')
end
Output
needs the editor renderer

In the app the file is written and the first message is shown.

Notes

Writing files needs the MatLivre editor's renderer. In the headless command-line runtime the call fails, which is why the example catches the error.

See also

saveas print figure gca imwrite