Programming |
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:
diary
file, and then edit the file in a text editor.
save
function.
fwrite
, fprintf
, ...
).
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:
load
.
fread
, fscanf
, ...
).
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.
Note
Saving with the -append switch does not append additional elements to an array that is already saved in a MAT-file. See the example below.
|
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.
Program Control | Files and Filenames |
© 1994-2005 The MathWorks, Inc.