| MATLAB® | ![]() |
| On this page… |
|---|
Saving Data from the Workspace Loading Data into the Workspace Viewing Variables in a MAT-File |
To save data from your workspace, you can do any of the following:
Copy from the MATLAB® Command Window and paste into a text file.
Record part of your session in a diary file, and then edit the file in a text editor.
Save to a binary or ASCII file using the save function.
Save spreadsheet, scientific, image, or audio data with appropriate function.
Save to a file using low-level file I/O functions (fwrite, fprintf, ...).
For more information: See Saving the Current Workspace in the MATLAB Desktop Tools and Development Environment documentation, and Using the diary Function to Export Data and Using Low-Level File I/O Functions in the MATLAB Programming Fundamentals documentation.
Similarly, to load new or saved data into the workspace, you can do any of the following:
Enter or paste data at the command line.
Create a script file to initialize large matrices or data structures.
Read a binary or ASCII file using load.
Load spreadsheet, scientific, image, or audio data with appropriate function.
Load from a file using low-level file I/O functions (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 in the MATLAB Programming Fundamentals documentation.
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');
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.
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.
A = [1 2 3 4 5]; B = 12.5; C = rand(4); save savefile; A = [6 7 8]; save savefile A -append;
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.
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 | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |