Saving Figures

Saving and Loading Graphs

You can save and reload graphs using the savefig and openfig functions. For example, create a bar graph and save it to a file called barGraph.fig (the .fig extension is added automatically):

savefig barGraph

You can reload the graph using openfig:

fig = openfig('barGraph');

MATLAB® creates a new figure, a new axes, and a new bar object using the same data as the original objects. Most of the property values of the new objects are the same as the original objects.

However, the Parent and Children properties now contain the new object handles. Also, MATLAB applies any default or system values that are different from those in the environment in which you saved the figure.

For example, suppose you set a default figure color after saving the barGraph.fig file. MATLAB uses the default when creating the new figure.

Accessing the New Object

To get the handle to the new bar object, use findobj and the figure handle returned by openfig:

h = findobj(fig,'Type','bar');

You can use h2 to set and get properties on the new bar object:

ans =


For more information on finding objects, see Find Objects

FIG-File Format

The MATLAB FIG-file is a binary format to which you can save figures so that they can be opened in subsequent MATLAB sessions. The whole figure, including graphs, graph data, annotations, data tips, menus and other uicontrols, is saved. (The only exception is highlighting created by data brushing.) These files have a .fig filename extension.

If you want to save the figure in a format that can be used by another application, see Saving to a Different Format — Exporting Figures.

Saving Figures From the Menu

To save a graph in a figure file,

  1. Select Save from the figure window File menu or click the Save button on the toolbar. If this is the first time you are saving the file, the Save As dialog box appears.

  2. Make sure that the Save as type is MATLAB Figure (*.fig) on the drop-down menu.

  3. Specify the name you want to give to the figure file.

  4. Click OK.

The graph is saved as a figure file (.fig), which is a binary file format used to store figures.

You can also use the saveas command.

Use the savefig command to create backward compatible FIG-files.

Opening a Figure File

To open a figure file, perform these steps:

  1. Select Open from the File menu or click the Open button on the toolbar.

  2. Select the figure file you want to open and click OK.

    The figure file appears in a new figure window.

You can also use the open command.

Saving to a Different Format — Exporting Figures

To save a figure in a format that can be used by another application, such as the standard graphics file formats TIFF or EPS, perform these steps:

  1. Select Export Setup from the File menu. This dialog provides options you can specify for the output file, such as the figure size, fonts, line size and style, and output format.

  2. Select Export from the Export Setup dialog. A standard Save As dialog appears.

  3. Select the graphic format from the list of formats in the Save as type drop-down menu. This selects the format of the exported file and adds the standard filename extension given to files of that type.

  4. Enter the name you want to give the file, less the extension.

  5. Click Save.

Export from Save As Dialog

You can use the Save As dialog to export a figure to a standard file format:

  1. Select Save As from the File menu.

  2. Enter a file name in the File name text field

  3. Select a file type from the Save as type drop-down menu.

  4. Click Save.

You can also use the saveas function to export figure to specific file formats. However, the saveas function and the Save As dialog do not produce identical results:

  • The Save As dialog produces images at screen resolution and at screen size.

  • The saveas function uses the default resolution of 150 DPI and honors the figure PaperPosition and PaperPositionMode properties to determine the size of the image.

Copying a Figure to the Clipboard

On Microsoft® systems, you can also copy a figure to the clipboard and then paste it into another application:

  1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears.

  2. Complete the fields on the Copying Options page and click OK.

  3. Select Copy Figure from the Edit menu.

    The figure is copied to the Windows® clipboard. You can then paste the figure from the Windows clipboard into a file in another application.

Printing Figures

Before printing a figure,

  1. Select Print Preview from the File menu to set printing options, including plot size and position, and paper size and orientation.

    The Print Preview dialog box opens.

  2. Make changes in the dialog box. Changes you can make are arranged by tabs on the left-hand pane. If you want the printed output to match the annotated plot you see on the screen exactly,

    1. On the Layout tab, click Auto (Actual Size, Centered).

    2. On the Advanced tab, click Keep screen limits and ticks.

    For information about other options for print preview, click the Help button in the dialog box.

To print a figure, select Print from the figure window File menu and complete the Print dialog box that appears.

You can also use the print command.

Generating a MATLAB File to Recreate a Graph

You can generate a MATLAB file from a graph, which you can then use to reproduce the graph. This feature is particularly useful for capturing modifications you make using the plot tools.

  1. Select Generate Code from the File menu.

    The generated code displays in the MATLAB Editor.

  2. Save the file using Save As from the Editor File menu.

Running the Saved File

Generated files do not store the data necessary to recreate the graph, so you must supply the data arguments. The data arguments do not need to be identical to the original data. Comments at the beginning of the file state the type of data expected.

For example, the following statements illustrate a case where three input vectors are required.

function createfigure(yvector1)
   %  YVECTOR1:  bar yvector
   %  Auto-generated by MATLAB on 08-Jul-2014 17:19:46
   % Create figure
   figure1 = figure;
   % Create axes
   axes1 = axes('Parent',figure1,'XTick',[1 2 3 4 5]);
   % Create bar
Was this topic helpful?