MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Using Variables

Contents

Storing Values

Like most programming languages, Matlab let you use variables to store values. In Matlab you do not need to declare variables or state their types, just jump in and use them. For example, try the following:

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.

Accessing Stored Values

Once you've stored one or more values in variables, you can recall them at any time by using the variable names. Below is a very simple example of a session where we store some values in variables and then use the variables later in a calculation:

width = 5
width =

     5

length = 10
length =

    10

area = length * width
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.

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:

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 =

     7

ans + 9
ans =

    16

Viewing Existing Variables

The easiest way to see or manipulate your variables is to utilize the window in the upper right-hand corner called "Worskpace". Here is how it might look:

You can see that we have explicitly defined two variables, x and y. The implicitly created variable ans appears here as well. You can see the values of the variables in the second column. The columns labelled Min and Max become useful when your variables actually represent larger matrices. (Min and Max will show you the smallest and largest value in the matrix, respectively.)

Modifying Existing Variables

You can change the contents of a variable at any time, by simply assigning it a new value at the command prompt. For example, if the variable x currently holds the value 10, we can replace that with the value 77 by typing:

x = 77
x =

    77

We can also do something that might confuse the math majors at first. Let's do:

x=x+1
x =

    78

Do you see what this does? In mathematics you think of this as an equation. 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 78 and then this is assigned to the left side x. The result of this line is that x is now 78. We can even do more:

x=x+2
x =

    80

Now x is 80.

x=x/2
x =

    40

Now x is 40. And so on.

That works well for a variable that represents just a one-by-one matrix. But later you'll be using much larger matrices, and you might want to modify just some of the values in the matrix. The best way to do that is to double-click the variable that you see in the Workspace window. This brings up the variable editor, which displays a grid where you can easily modify the contents of a variable. Try it! Below is an example of how this looks for our variable x:

If we wanted to change x into a larger matrix with additional values, we could simply enter them here. Try it!

Clearing the Variable List

If you'd like to start from scratch and eliminate all of the variables that you've created, just type the clear command:

clear

Be sure that you really don't need the values of the variables before you do this!

Saving and Loading the Variable List

It is easy to store the values of all of your current variables so that you can use them during another Matlab session later in your life. Below is an example of the command you would use to store all of your current variables in a file called MyVars:

save MyVars

Try creating a few variables and then saving them. The variables and their current values are all stored in a file called MyVars.mat. They can all be retrieved at any time (typically during a different Matlab session) by using the command load. If you saved your variables as MyVars, you could retrieve them all at a later date using:

load MyVars

Self-Test

  1. Calculate the number of seconds in an hour by entering 60 * 60.
  2. Calculate the number of seconds in a day, making use of your previous answer, ans.
  3. Create variables called secondsPerYear and initialize it with the correct value making use of your previous answer.
  4. Create another variable called myAge and set it equal to your age, in years.
  5. Save the variables you created in a file called yearVars.
  6. Clear all existing variables. [Look in the Workspace pane to be sure that the variables are really gone.]
  7. Bring back the variables that you stored in the file yearVars.
  8. Using your previous variables, calculate your approximate age, in seconds.
  9. How old is a person when they reach the age of a billion seconds?

Answers to Self-Test

Next Topic: Symbolic Variables