Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Student Center

Saving Variables and Sessions



Introduction
In the previous lesson, you learned how you could type and save a sequence of MATLAB commands in a text file, and run these commands by typing the name of the text file at the command prompt. In this lesson, you will learn how to save all or some of the variables you have defined during a MATLAB session, and then load them in a later MATLAB session (MAT-files). You will also learn how you can save the contents of all or part of the MATLAB command window in an ASCII text file, called a diary file, so that you can view the file in a text editor, or print it out.

Saving Variables
Let's start with saving variables. So, say you have been typing commands and running scripts in the MATLAB command window for a while, and you have defined a lot of variables. You're rushing to finish up your project, but you still have more variables to define and other commands to run before you finish. Unfortunately, you have to get to class in 5 minutes, and so there is no way you are going to finish in one sitting. What you need is some way to save all your variables, and then be able to load them back in after class when you will be able to work on your project again. This is precisely what the save command does for you.

The save command can be used to save all or only some of your variables into a MATLAB data file type called MAT-file. If you want to choose the name of the file yourself, you can type save followed by the filename you want to use. MATLAB will then save all currently defined variables in a file named with the name you chose followed by the suffix ".mat" (for example, if you chose the name my_variables MATLAB would save as "my_variables.mat" in your present working directory). Before saving you should change your present working directory to one of your own directories (such as some directory on your floppy diskette), or specify the complete path to where you want MATLAB to save your variables (for example "a:\my_variables\my_vars"). Let's see some examples of using save:

clear
who
cd c:\my_variables
pwd % present working directory

ans =
c:\my_variables

a = 10;
b = 20;
c = 30;
d = sqrt((a + b + c)/pi);
d

d = 4.3702

who

Your variables are:
a ans b c d

save my_chosen_filename
dir

. .. my_chosen_filename.mat

clear
who

The above use of the save command saved all the MATLAB workspace defined variables. If you just want to save some of your variables, you simply list the variables you want to save after typing, save and the filename. For example, here is how you would save only the variables a and c:

clear
who
a = 10;
b = 20;
c = 30;
who

Your variables are:
a b c

pwd

ans =
c:\my_variables

save some_of_my_variables a c
dir

. .. some_of_my_variables.mat my_chosen_filename.mat

clear
who

Loading Variables
So now we have seen how we can save all or some of the defined variables. That alone is not very useful unless we can load them back in later to use them again. This is where the load command is used. Typing load followed by a filename (without the ".mat" suffix) will search the MATLAB path (refer to the next lesson regarding the MATLAB path) for the file, "filename.mat", and load all the variables saved in that file (for example, typing load my_vars would cause MATLAB to search for "my_vars.mat" and load the variables saved in it). So, let's see some examples of loading variables back into MATLAB.

clear
who

cd c:\my_variables
dir

. .. some_of_my_variables.mat my_chosen_filename.mat

load my_chosen_filename
who

Your variables are:
a ans b c d

a

a = 10

clear
who
load some_of_my_variables
who

Your variables are:
a c

c

c = 30

You can also choose to load in only some of the variables that are saved in a MATLAB data file (MAT-file). To load only some of the variables saved in a file back into MATLAB, just type the names of the variables you want loaded back in after typing load and the filename (without ".mat") at the command prompt. For example, if variables "a", "ans", "b", "c", and "d" are all saved in a file, then you could use the load command to load only "a" and "c" back in:

who
dir

. .. some_of_my_variables.mat my_chosen_filename.mat

whos -file my_chosen_filename

Name Size Bytes Class
       
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
d 1x1 8 double array

Grand total is 4 elements using 32 bytes
load my_chosen_filename a c
who

Your variables are:
a c

a

a = 10

Saving the Text of MATLAB Sessions
Now let's move on to saving the text of a MATLAB session. To do this, you use the diary command. If you type diary followed by a filename, then MATLAB will append everything you type, and its results (i.e., what MATLAB returns as results) to the file, filename. To stop text from being appended to the file type, diary off, at the command prompt; everything in the command window after, diary off, will not be appended to the file. You can also turn the diary back on by typing, diary on, to start having text appended to the file again.

Here is an example of using the diary command (note closely what was included in the diary file, which is shown below with the "type" command --- for example, note that the command prompt "" is not included in the diary file):

cd c:\my_diaries

dir

. ..

diary test_diary
dir

. .. test_diary

pwd

ans =

c:\my_diaries

who

Your variables are:

ans

help sqrt

SQRT Square root.
SQRT(X) is the square root of the elements of X. Complex
results are produced if X is not positive.

See also SQRTM.

Overloaded methods
help sym/sqrt.m

diary off
This will not be in the diary file!!!
??? This will
|
Missing operator, comma, or semi-colon.

clear
help ans

ANS Most recent answer.
ANS is the variable created automatically when expressions
are not assigned to anything else. ANSwer.

diary on
who
help abs

ABS Absolute value.
ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.

See also SIGN, ANGLE, UNWRAP.

Overloaded methods
help sym/abs.m

diary off
dir

. .. test_diary

type test_diary

dir

. .. test_diary

pwd

ans =

c:\my_diaries

who

Your variables are:

ans

help sqrt

SQRT Square root.
SQRT(X) is the square root of the elements of X. Complex
results are produced if X is not positive.

See also SQRTM.

Overloaded methods
help sym/sqrt.m

diary off
who
help abs

ABS Absolute value.
ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.

See also SIGN, ANGLE, UNWRAP.

Overloaded methods
help sym/abs.m

diary off

Continue on to the next lesson.

Contact sales
Software evaluation for instructors
Free technical kit
E-mail this page

MATLAB & Simulink Student Version

Includes full-featured versions of both MATLAB and Simulink