Programming Previous page   Next Page

Save and Load

This section covers the following topics:

Saving Data from the Workspace

To save data from your workspace, you can do any of the following:

For more information: See Saving the Current Workspace in the MATLAB Desktop Tools and Development Environment documentation, Using the diary Function to Export Data, and Using Low-Level File I/O Functions.

Loading Data into the Workspace

Similarly, to load new or saved data into the workspace, you can do any of the following:

For more information: See Loading a Saved Workspace and Importing Data in the MATLAB Development Environment documentation, and Using Low-Level File I/O Functions.

Viewing Variables in a MAT-File

To see what variables are saved in a MAT-file, use who or whos as shown here (the .mat extension is not required). who returns a cell array and whos returns a structure array.

Appending to a MAT-File

To save additional variables to an existing MAT-file, use

Any variables you save that do not yet exist in the MAT-file are added to the file. Any variables you save that already exist in the MAT-file overwrite the old values.

In this example, the second save operation does not concatenate new elements to vector A, (making A equal to [1 2 3 4 5 6 7 8]) in the MAT-file. Instead, it replaces the 5 element vector, A, with a 3 element vector, also retaining all other variables that were stored on the first save operation.

Save and Load on Startup or Quit

You can automatically save your variables at the end of each MATLAB session by creating a finish.m file to save the contents of your base workspace every time you quit MATLAB. Load these variables back into your workspace at the beginning of each session by creating a startup.m file that uses the load function to load variables from your MAT-file.

For more information: See the startup and finish function reference pages.

Saving to an ASCII File

When you save matrix data to an ASCII file using save -ascii, MATLAB combines the individual matrices into one collection of numbers. Variable names are not saved. If this is not acceptable for your application, use fprintf to store your data instead.

For more information: See Exporting Delimited ASCII Data Files.


Previous page  Program Control Files and Filenames Next page

© 1994-2005 The MathWorks, Inc.