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 you can save all or some of the variables you have defined during a MATLAB session, and then load and use these saved variables in a later MATLAB session. 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; you can then view this diary file in a text editor or word processor, 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. Typing "save" by itself at the command prompt will save all currently defined variables into a file called "matlab.mat" inside the present working directory. 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 the present working directory 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 the present working directory). Note that, you should change your present working directory to one of your own directories (such as some directory on your thumb drives), or specify the complete path to where you want MATLAB to save your variables such as on your H:drive (for example "H:\my_variables\my_vars"). Let's see some examples of using "save":

 

>> clear
>> who
>> cd H:\my_variables
(ASSUMING YOU ALREADY HAVE A DIRECTORY CALLED “my_variables” AT THE TOP OF YOUR H:drive DIRECTORY TREE)

 

>> pwd

ans =

H:\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

Saving to: matlab.mat

>> save my_chosen_filename
>> dir

. matlab.mat
.. my_chosen_filename.mat

>> clear
>> who
>>

 

The above uses of the "save" command saved all 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 (note that to save only some of your variables, you have to specifically give a filename). For example, here is how you would save only some of the defined variables:

 

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

Your variables are:

a b c

>> pwd

ans =

H:\my_variables

>> save some_of_my_variables a c
>> dir

. matlab.mat 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. Well, that is, surprise, surprise, what the "load" command does. Typing "load" by itself at the command prompt will search the present working directory, and any paths in matlabpath, for the file whose name is "matlab.mat", and then load all the variables saved inside "matlab.mat". Typing "load" followed by a filename (without the ".mat" suffix) will search the present working directory, and any paths in matlabpath, for the file whose name is the filename with the suffix ".mat" appended, and then 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:

 

>> who
>> addpath H:\my_variables
>>load

Loading from: matlab.mat

>> who

Your variables are:

a ans b c d

>> a

a =

10

>> clear
>> who
>>

 

>> cd H:\my_variables

>> dir

. matlab.mat 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 load in only some of the variables that are saved in a 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

. matlab.mat some_of_my_variables.mat
.. my_chosen_filename.mat

>> 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, which is quite easy to use. If you type "diary" followed by a filename, then MATLAB will append everything you type, as you type it, and the results of what you type (i.e., what MATLAB returns as results) to a file in the present working directory whose name is filename. You can also stop text from being appended to the file by typing "diary off" at the command prompt; everything in the command window after "diary off" will not be appended to the file, until you later type "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):

 

>> pwd

ans =

H:\ [OR WHERE EVER YOU HAVE SET YOUR PATH TO BE USING the “cd” COMMAND OR THE DROP DOWN DIRECTORY TREE]


>> diary test_diary


>> dir

. .. test_diary

>> pwd

ans =

H:\

>> 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 [N.B. This line will be in the diary file!!!  But your diary is now OFF and anything more you type WILL NOT be recorded]

» clear
» help ans

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

» diary on [N.B. This line will not be in the diary file!!! Your diary is now ON and everything you type WILL be recorded]

» 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 [N.B. This line will be in the diary file!!! Your diary is now OFF and anything more you type WILL NOT be recorded]


>> 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

Also, you can view the contents of this diary file by opening it up using any ASCII editor such as “Notepad.”


To continue on to the next lesson, click here.