Documentation

Save, Load, and Delete Workspace Variables

The workspace is not maintained across sessions of MATLAB®. When you quit MATLAB, the workspace clears. However, you can save any or all of the variables in the current workspace to a MAT-file (.mat). You can load MAT-files at a later time during the current MATLAB session, or during another session, if you want to reuse the workspace variables.

The following table describes how to save, load, and delete workspace variables.

ActionDesktop WorkflowProgrammatic Workflow

Save all workspace variables to a MAT-file

On the Home tab, in the Variable section, click Save Workspace.

Use the save function. For example, save all current workspace variables to the file june10.mat:

save('june10')

Save selected variables to a MAT-file

Do one of the following:

  • Select the variables in the Workspace browser, right-click, and then select Save As.

  • Drag variables from the Workspace browser to the Current Folder browser.

Use the save function. For example, save only variables A and B to the file june10.mat:

save('june10','A','B')

Save part of a variable

(None)

Use the matfile function. For an example, see Save Parts of Variables to MAT-Files.

Load a MAT-file

Select the MAT-file in the Current Folder browser, right-click, and then select Load.

Use the load function. For example, load all variables from the file durer.mat:

load('durer')

Load selected variables from a MAT-file

Do one of the following:

  • On the Home tab, in the Variable section, click Import Data.

    Select the MAT-file you want to load and click Open.

  • In the Current Folder browser, select the MAT-file that contains the variables. Drag variables from the Details panel of the Current Folder browser to the Workspace browser.

Use the load function. For example, load variables X and map from the file durer.mat:

load('durer','X','map')

Load part of a variable

(None)

Use the matfile function. For an example, see Load Parts of Variables from MAT-Files.

Delete all variables in the workspace

On the Home tab, in the Variable section, click Clear Workspace.

Use the clear function.

clear

Delete selected variables from the workspace

Select the variables in the Workspace browser, right-click, and then select Delete.

Do one of the following:

  • Delete specified variables using the clear function. For example, clear variables A and B:

    clear A B
  • Preserve specified variables, but delete others, using the clearvars function with the -except option.

    clearvars -except A

    Caution   When you load data into the MATLAB workspace, the new variables you create overwrite any existing variables in the workspace that have the same name.

You also can save workspace variables to a MATLAB script:

  • To save all workspace variables, on the Home tab, click Save Workspace.

  • To save selected workspace variables, select the variables in the Workspace browser, right-click, and then select Save As.

Then, in the Save As window, specify a file name. In the Save as type menu, select MATLAB Script.

Variables that cannot be saved to a script are saved to a MAT-file with the same name as that of the script.

To load the saved variables into the workspace, simply run the script.

Related Examples

Was this topic helpful?