Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Save and Load

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, and Writing to Text Data Files with Low-Level I/O in the MATLAB Data Import and Export documentation.

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 Importing Data in the MATLAB Programming Fundamentals documentation.

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.

mydataVariables = who('-file', 'mydata.mat');

Appending to a MAT-File

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

save matfilename -append

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.

A = [1 2 3 4 5];   B = 12.5;   C = rand(4);
save savefile;
A = [6 7 8];
save savefile A -append;

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 Writing to Delimited Data Files.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS