Table of Contents
Why Saving Figures Matters
When you create a figure in MATLAB, it exists only in memory and in a figure window. If you close MATLAB or the figure window, it is gone. Saving and exporting figures allows you to keep your work for later, share results, or include plots in reports, slides, or articles.
In this chapter you focus on saving figure content to files, choosing formats, and controlling the quality and size of the output.
Saving Figures in MATLAB Figure Format
MATLAB has its own figure file format with the extension .fig. This format stores the figure as an object that you can later reopen and edit interactively.
To save the current figure from the Command Window you can use:
savefig('myPlot.fig')If you have multiple figures open and you want to save a specific one, pass its handle:
f = figure;
plot(1:10);
savefig(f, 'linePlot.fig');
You can later reopen a .fig file with:
openfig('linePlot.fig');When you open the file, MATLAB recreates the figure as if you had just plotted it, with its axes, labels, legend, and all properties still editable. This format is usually the best choice if you want to modify the figure later inside MATLAB.
Using the Save Button and Figure Menus
The figure window provides graphical controls for saving and exporting. These are convenient if you prefer not to type commands.
In a typical figure window you can click the Save icon in the toolbar. If you save as a .fig file, MATLAB stores the complete figure in its native format. If you choose another format, MATLAB exports an image or a document version of the figure.
You can also use the figure menu. In newer versions there is often a menu item like File > Save or File > Save As and sometimes File > Export Setup or similar. Save or Save As is usually used for .fig files, while the export options allow you to choose formats such as PNG, JPEG, or PDF. The exact menu names can depend on your MATLAB version, but the idea is the same. You select the output format in the file type dropdown, specify a filename, and click save.
This interactive approach is useful when you adjust the graphics by hand and then save only when you are satisfied with the appearance.
Image Formats vs Document Formats
When you export a figure, you must choose a file type. There are two broad groups that are commonly used.
Image formats store the figure as pixels. Common formats are PNG (.png), JPEG (.jpg or .jpeg), and TIFF (.tif or .tiff). These are raster images. They are suitable for web pages, presentations, and quick sharing. PNG is usually preferred for plots with sharp lines and text, because it uses lossless compression. JPEG is more suitable for photographs and may blur lines and text because it uses lossy compression.
Document or vector formats store shapes and text as geometric objects instead of fixed pixels. Common formats are PDF (.pdf), EPS (.eps), and sometimes SVG (.svg) in some MATLAB versions. These formats are usually preferred for scientific articles and high quality printing. Vector graphics can be scaled without losing sharpness, so lines and text remain clear when you zoom.
A typical choice is PNG for slides and PDF for reports or manuscripts that you will print or share as documents.
Basic Export with `saveas`
The simplest way to export a figure from the Command Window is with the saveas function. Its basic form is:
saveas(figureHandle, filename, format)
The figureHandle is usually a variable that stores the handle returned by figure, or gcf which means "current figure". The filename is a character vector or string that does not need the extension if you specify format. The format is a short code that tells MATLAB what type of file to produce.
For example, to save the current figure as a PNG image:
saveas(gcf, 'myPlot', 'png');
To save a figure handle f as a PDF:
f = figure;
plot(magic(3));
saveas(f, 'magicPlot', 'pdf');If you omit the format and just specify a file name with an extension, MATLAB tries to infer the format from that extension:
saveas(gcf, 'myPlot.png');
saveas(gcf, 'myPlot.pdf');This is convenient but using explicit format codes makes your code clearer, especially if you share it.
Using `exportgraphics` for Better Control
For many tasks, especially in recent MATLAB versions, exportgraphics is recommended instead of saveas. It gives more consistent results and better control over resolution, background, and size.
The function is called on an axes or other graphics object, not necessarily on the whole figure. The simplest usage is:
exportgraphics(gca, 'myAxes.png');
Here gca is the current axes. To export the whole figure, you can pass gcf:
exportgraphics(gcf, 'myFigure.pdf');
You can control the resolution with the Resolution name-value argument, given in dots per inch (dpi):
exportgraphics(gcf, 'highResPlot.png', 'Resolution', 300);Higher resolution values produce larger files but sharper images when printed or zoomed. Many journals and printers expect at least 300 dpi.
You can also control transparency. For some formats, such as PNG, you may want a transparent background so that the image blends with slide or web page colors:
exportgraphics(gca, 'transparentPlot.png', 'BackgroundColor', 'none');
exportgraphics tries to match what you see on the screen, so it is generally reliable when you adjust the figure interactively and then export.
Exporting Only Parts of a Figure
Sometimes you do not want the entire figure, but only a specific set of axes or a subfigure. exportgraphics makes this straightforward, because it can export any graphics object that has a handle.
For example, if you have a tiled layout or multiple axes, you can capture individual parts:
t = tiledlayout(2,2);
ax1 = nexttile;
plot(ax1, 1:10, rand(1,10));
ax2 = nexttile;
plot(ax2, 1:10, rand(1,10).^2);
exportgraphics(ax1, 'topLeftPlot.png');
exportgraphics(t, 'fullGrid.pdf');
In this code, ax1 is a single axes, and t is the tiled layout container. The first export creates an image with only the top left plot. The second export creates a document file with the entire grid of subplots.
This is useful when you want to reuse one panel from a complex figure in a different context.
Controlling Figure Size for Export
The appearance of an exported figure depends on both its resolution and its size. In many cases you want to control the size in physical units such as centimeters or inches.
One common approach is to set the figure units and position before exporting. The Position property of a figure is given in the order [left bottom width height]. If the Units are set to inches, for example, then width and height are measured in inches.
Consider the following example:
f = figure;
plot(1:10, rand(1,10));
f.Units = 'inches';
f.Position = [1 1 4 3]; % 4 inches wide, 3 inches high
exportgraphics(f, 'smallPlot.png', 'Resolution', 300);With this code you obtain a 4 by 3 inch image at 300 dpi, which is often suitable for printed documents.
The same idea applies if you use centimeters. In that case, you can set Units to 'centimeters' and adjust the position accordingly.
If you are using saveas, it also uses the figure size on screen, but you do not have as many direct controls for resolution. In that case, either adjust the screen size carefully or prefer exportgraphics for precise control.
Exporting for Presentations and Documents
When preparing slides in software like PowerPoint or similar tools, PNG images are typically a good choice. They have clear lines and support transparent backgrounds. A quick way to create a slide-ready image is:
exportgraphics(gca, 'slidePlot.png', ...
'Resolution', 150, ...
'BackgroundColor', 'none');A resolution of 150 dpi is usually enough for slides viewed on standard projectors or monitors. The transparent background option allows the slide software to show its own background color through.
For documents such as reports in PDF format, exporting figures as PDF or EPS often yields higher quality results. A typical call is:
exportgraphics(gcf, 'reportFigure.pdf');When you insert the PDF into a document, the text and lines remain sharp even when you zoom.
If you write documents in LaTeX, EPS can also be used for inclusion in older workflows. However, PDF is usually more convenient and is supported by many LaTeX setups as well.
Automating Figure Export in Scripts
When you run analysis scripts that create many figures, it is often useful to save each figure automatically without manual interaction. This lets you repeat the analysis and always obtain up to date plots.
A simple pattern uses a loop that creates figures and calls exportgraphics or saveas for each one:
for k = 1:5
f = figure;
plot(1:10, rand(1,10) * k);
title(['Run ' num2str(k)]);
filename = ['run' num2str(k) '.png'];
exportgraphics(f, filename, 'Resolution', 200);
close(f);
endThis code creates five plots, saves each of them to a separate PNG file, and then closes the figure window. Closing figures is important in loops so that you do not end up with many open windows and excessive memory use.
If you want to use savefig for later editing, you can modify the loop:
for k = 1:5
f = figure;
plot(1:10, rand(1,10) * k);
savefig(f, ['run' num2str(k) '.fig']);
end
You can later open any of these .fig files and work with them interactively.
Common Pitfalls When Saving Figures
There are a few issues that often confuse beginners when exporting figures.
One common issue is that the saved figure does not look like what you saw on the screen. This can happen if the export function ignores some on screen properties or if the figure is resized automatically. exportgraphics is generally more consistent than saveas, so prefer it when you care about precise appearance.
Another issue is blurry text or jagged lines. This usually results from low resolution or from saving as a compressed image format like JPEG. For crisp plots, use PNG, specify a higher resolution, or use a vector format like PDF where practical.
File paths are another source of confusion. If you write exportgraphics(gcf, 'plot.png'), MATLAB saves the file in the current folder. If you need to save to another folder, provide a full or relative path, for example:
exportgraphics(gcf, 'results/plot.png');Make sure that the folder exists and that MATLAB can write to it.
Finally, be careful not to overwrite files unintentionally. MATLAB will overwrite existing files without asking if you use commands like exportgraphics or saveas. Using different filenames, or adding timestamps to filenames, helps avoid this problem.
Important points to remember:
Use .fig with savefig when you want to reopen and edit the figure later in MATLAB.
Use exportgraphics for consistent, high quality output and better control over resolution and size.
Choose PNG for screen presentations, PDF or other vector formats for documents and printing.
Control figure size by setting the figure Units and Position before exporting if you need specific dimensions.
When automating exports in scripts, always specify filenames clearly and close figures you no not need to keep open.