To save a figure in MATLAB, you can use the 'Save' option from the figure window's File menu, or use functions like saveas
or exportgraphics
in the command window or a script.
Saving your MATLAB figures allows you to store visualizations for later use, sharing, or inclusion in documents. MATLAB offers several ways to save figures, depending on whether you want to preserve it as an interactive MATLAB figure or export it as a standard image or document format.
Saving as a MATLAB Figure File (.fig)
Saving a figure as a .fig
file preserves the figure in its current state, including axes properties, data, and any controls or UI elements. You can open a .fig
file later in MATLAB and continue working with it.
-
Using the File Menu: The simplest way is to use the graphical interface.
- With the figure window active, go to the File menu.
- Select Save or Save As....
- Choose a location and filename. The default format is typically
.fig
. - Click Save.
-
Understanding
.fig
File Options: When working with.fig
files, MATLAB allows configuration related to how these files are handled. As highlighted in reference information, you might encounter settings related to "MAT and Fig Files save format options" within MATLAB's preferences, often found under general settings. These options can influence how data or specific properties within the figure are saved into the.fig
file, ensuring compatibility or optimizing file size depending on your needs.
Saving as an Image or Document File
For sharing figures outside of MATLAB or including them in reports and presentations, you'll typically save them in standard image formats (like PNG, JPG, TIFF) or document formats (like PDF, EPS).
-
Using the
saveas
Function: This function saves the figure to a specified file format.% Get the current figure handle (if needed, or it uses the current figure) fig_handle = gcf; % Save as a PNG image saveas(fig_handle, 'my_figure.png'); % Save as a JPEG image saveas(fig_handle, 'my_figure.jpg'); % Save as a PDF document saveas(fig_handle, 'my_figure.pdf'); % Save as an EPS file (often good for publications) saveas(fig_handle, 'my_figure.eps');
You can specify the format by the file extension.
saveas
supports many formats, includingpng
,jpg
,bmp
,tiff
,eps
,pdf
,emf
,svg
, andfig
. -
Using the
exportgraphics
Function: Introduced in later MATLAB versions,exportgraphics
is generally recommended oversaveas
for generating high-quality output for documents or external use. It provides more control over resolution, anti-aliasing, and transparency.% Get the current figure handle fig_handle = gcf; % Save as a high-resolution PNG image (300 DPI) exportgraphics(fig_handle, 'high_res_figure.png', 'Resolution', 300); % Save as a PDF for print, preserving vector graphics where possible exportgraphics(fig_handle, 'vector_figure.pdf', 'ContentType', 'vector'); % Save as a JPG with specific quality setting exportgraphics(fig_handle, 'compressed_figure.jpg', 'Resolution', 150, 'Quality', 90);
exportgraphics
supports formats likepng
,jpg
,tiff
,pdf
, andeps
.
Saving the Figure Object
Less commonly, you might want to save the figure object itself as a variable in a .mat
file. This saves the properties and data associated with the figure handle, but not necessarily the visual representation directly viewable outside MATLAB.
% Get the current figure handle
fig_handle = gcf;
% Save the figure handle variable to a .mat file
save('figure_data.mat', 'fig_handle');
You can then load this .mat
file later and access the fig_handle
variable.
Summary of Common File Formats
Here's a quick look at common formats:
Format | Extension(s) | Description | Best For |
---|---|---|---|
MATLAB Figure | .fig |
Preserves interactive figure, editable in MATLAB. | Saving work-in-progress figures. |
PNG | .png |
Raster image, supports transparency, lossless. | Web, presentations, general image files. |
JPEG | .jpg , .jpeg |
Raster image, compressed, lossy. | Photos, web graphics (smaller file size). |
.pdf |
Document format, can contain vector and raster. | Documents, sharing, printing. | |
EPS | .eps |
Vector format, often used in LaTeX/publishing. | Publications, high-quality print. |
TIFF | .tif , .tiff |
Raster image, supports layers, lossless options. | High-quality print, archival. |
Choosing the right format depends on your intended use for the saved figure. For further editing in MATLAB, .fig
is ideal. For documents and presentations, exportgraphics
to PDF or PNG is usually recommended.