Introduction

 

Variables are a fundamental concept in MATLAB, and you will use them all the time. Basically, a variable is a holding place for a value which you can give a name to. The point of this is that, when calculating something new later, you can use the value that a variable refers to as part of the new calculation. Going through some examples will make this clear, so let's do that now. In its simplest mode of use, MATLAB can be used just like a pocket calculator. For example, here is how you would do some simple, calculator-like operations with MATLAB:

 

>> 4 + 10

ans =

14

>> 5 *10 + 6

ans =

56

>> (6 + 6) / 3

ans =

4

>> 9^2

ans =

81

>>

 

The "ans" Variable

 

As you can see, MATLAB supports all the basic arithmetic operations: +, -, *, /, ^, etc.; and you can group and order operations by enclosing them in parentheses. However, what exactly is "ans" above? You might think it is just MATLAB's way of telling you "the answer is ..." Well, that is part of it, but the complete answer is that "ans" is a variable. Whenever you type in some mathematical expression, like "4 + 5", MATLAB puts the result value of that expression into a variable called "ans". You can refer to that value by just typing "ans":

 

>> 4 + 5

ans =

9

>> ans

ans =

9

>>

 

However, if you then type another mathematical expression, the old value of "ans" will be discarded and the value of the new mathematical expression will replace it. But, you can make use of the old value of "ans" in your new mathematical expression. For example, continuing from above, "ans" currently has a value of 9 which is changed here:

 

>> ans + 10

ans =

19

>> ans

ans =

19

>>

 

Defining Your Own Variables

 

The "ans" variable by itself isn't that useful, but the real power is that you can define and use your own variables. For example:

 

>> a = 10

a =

10

>> b = 20

b =

20


>> c = 30

c =

30

>> a

a =

10

>> the_average = (a + b + c) / 3

the_average =

20

>>

 

Up to now, we have seen how variables can be assigned and used when typing commands into the MATLAB command window. This is useful, but variables are more commonly used inside script files, which are text files containing sequences of MATLAB commands that can be run altogether, at one time, just as if you had typed all the commands in the sequence into the MATLAB command window yourself. A later lesson will go into the details of creating and using script files, but here let's continue on with some more commands dealing with variables.

 

Listing Currently Defined Variables and Clearing Variables

 

Let's say you have been typing for a long time into the MATLAB command window, and have defined a lot of different variables. You probably can't remember all the variable names you have defined, and so it would be nice to get a list of all the variables currently defined. This is exactly what the "who" command does. Simply typing "who" at the command prompt will return to you the names of all variables that are currently defined. For example (the "clear" command is explained below --- for now, if you are trying out this example, just type it as it is and read about "clear" below):

 

>> clear


>> who

>> 


>> a = 5

a =

5

>> b = 6

b =

6

>> who

Your variables are:

a b

>>

 

Now let's say you have defined a lot of variables for some project you are working on. But, you want to start working on another project, and have another set of variables for that new project. In order to avoid accidentally using the old variables in the new project, it would be nice if you could get rid of all the old variables and start over with a "clean slate". This is exactly what the "clear" command will do. Typing "clear" at the command prompt will remove all variables and values that were stored up to that point. For example, continuing from the above example:

who

Your variables are:

a b

>> clear
>> who
>>

Hiding Results with Semicolons

 

Semicolons typed after commands can be used to hide the printing of results. If you type an expression (such as "4 + 5" or "b = 4 + 5") and then follow it with a semicolon, then MATLAB will evaluate the expression and store the result internally, but it will not print out the results in the MATLAB command window for you to see. For example:

>> a = 10;
>> b = 20;
>> c = 30;
>> d = 40;
>> the_average = (a + b + c + d) / 4

the_average =

25

>> the_average;
>> b

b =

20

e = 50

e =

50

>> the_new_average = (a + b + c + d + e) / 5;
>> the_new_average

the_new_average =

30

>>

 

This might not seem to be very useful, but it is actually quite handy and used all the time. You will mainly be concerned only with some final result in your MATLAB sessions, which will be calculated by combining many temporary, intermediate variables. You likely won't need to or want to see the values of the temporary, intermediate variables (only the final result interests you), and appending a semicolon to the expressions that assign values to the temporary, intermediate variables causes their results to not be printed. For example, in the above example semicolons were typed after the definitions for the variables a, b, c, and d; only the final result, the average value of these four variables, was important and a semicolon was thus not added after the expression for "the_average" variable, causing its result to be printed.

Some Important Points Concerning Variables

A few more important points that should be noted before moving on are as follows. First, you can also assign pieces of text to variables, not just numbers. You do this using single quotes (not double quotes --- single quotes and double quotes have different uses in MATLAB) around the text you want to assign to a variable. For example:

 

>> some_text = 'This is some text assigned to a variable!';
>> some_text

some_text =

This is some text assigned to a variable!

>>

 

You will mostly just use text when assigning labels to plots (see the plotting lesson to learn about this), but you should be careful not to mix up variables that have text values with variables that have numeric values in equations. If you do this, you will get some strange results. For example, the variable "b" in the following MATLAB session is really a text string, and you get strange results if you try to use it as a number:

 

>> a = 5;
>> b = '5';
>> a/b

ans =

0.0943

>> a*b

ans =

265

>>

 

There are some specific rules for what you can name your variables, so you have to be careful. Basically, you can't have any spaces in your variable names, so, for example, using "this is a variable" as a variable name is not allowed, but "this_is_a_variable" is fine (in general, you can use the "_" character wherever you would use space to string words together in your variable name). Also, there are certain special characters and combinations of characters that you cannot use in your variable names; let's not go into details, but to make it simple, if you just make sure you only use primary alphabetic characters (i.e., "A-Z"), numbers, and the underscore character (i.e., "_") in your variable names then you will be fine. Finally concerning variables, MATLAB is case sensitive. What this means for variables is that the same text, with different mixes of capital and small case letters, will not be the same variables in MATLAB. For example, "A_VaRIAbLe", "a_variable", "A_VARIABLE", and "A_variablE" would all be considered distinct variables in MATLAB.

 

Error Messages

 

Finally, you will notice that if you mistype something at the command prompt, or in general type something that MATLAB doesn't understand, then MATLAB will return an error message to you. Basically, this is just some text, starting with "???", that MATLAB returns saying that it didn't understand your command, or that something is wrong with the way you typed it. Often you will know immediately what the problem is just by reading the error message. However, if you don't understand what MATLAB returned as an error message, you can try using the "help" command with selected words from the error message text that MATLAB returned. Here are some examples of getting error messages:

 

>> my variable = 5
??? my variable
|
Missing operator, comma, or semi-colon.

>> my_variable = 5

my_variable =

5

blah


??? Undefined function or variable 'blah'.

 

To continue on to the next lesson, click here.