Chapter 03 - Variables: Declaring Variables As Values
Chapter 03 - Variables: Declaring Variables As Values
Table of Contents
Declaring Variables as Values ............................................................................................... 1
Accessing Stored Values ...................................................................................................... 1
Altering Variables Depending on Themselves .......................................................................... 2
A Warning About Stored Variables ........................................................................................ 3
Using ans ......................................................................................................................... 3
Clearing the Variable List ..................................................................................................... 4
Semicolons at the end! ......................................................................................................... 4
Like most programming languages, Matlab let you use variables to store values.
x = 10
x =
10
This will create a variable called x (or re-use the existing one if you've previously assigned a value to x
during this session), storing 10 as its value. You can now use x in any calculation and it will be replaced
with the value 10.
You should know that in Matlab all of your variables are actually storing matrices, not just an individual
number. In the example above, the variable x is really being initialized with a one-by-one matrix that
contains the value 10. This observation will become important later.
width = 5
width =
length = 10
1
Chapter 03 - Variables
length =
10
area =
50
Of course the three variables (width, length and area) created above can all be used in any subsequent
calculations that we might want to do.
x=7
x =
and then
x=x+1
x =
Do you see what this does? In mathematics you may think of x=x+1 as an equation, and one with no
solution. however in this context in Matlab it's treated as an assignment. The way it works is that the right
side x+1 is evaluated to get 8 and then this is assigned to the left side x. The result of this line is that x
is now 8. We can even do more:
x=x+2
x =
10
Now x is 10.
x=x/2
2
Chapter 03 - Variables
x =
Using ans
By now you must have noticed that when you ask Matlab to do a calculation for you (and you are not
storing the result explicitly in a variable), Matlab responds with something like:
2*5
ans =
10
Conveniently, ans is itself a variable that you can use in subsequent calculations. It simply stores whatever
the result of the most recent calculation was. Here is a simple example of a session in which we make
use of ans
15 * 3 + 4
ans =
49
ans / 7
ans =
ans + 9
ans =
16
3
Chapter 03 - Variables
clear
Be sure that you really don't need the values of the variables before you do this!
a = 3*4
a =
12
and
a = 3*4;
In both cases a is assigned the value 12 but in the second case Matlab doesn't say anything about it. Very
often in Matlab you want something done and you want Matlab to be quiet about it. Notice in all the
following chapters when and why there are semicolons sometimes but not other times. Experiment and see!