Student Center
Creating Variables
- Prerequisites and Expectations for the Tutorial
- A Very Elementary MATLAB Tutorial
- Starting MATLAB
- Running the Demos
- Getting Help
- Creating Variables
- Performing Calculations
- Visualizing Data
- Creating Scripts with MATLAB Editor/Debugger
- Saving Variables and Sessions
- Working with Files, Directories and Paths
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? In short ans is short for "answer", and is used in MATLAB as the default variable name when none is specified. You can refer to that value by just typing ans:
4 + 5
ans = 9
ans
ans = 9
However, if a new equation is entered the value of ans would change.
ans + 10
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
Listing Currently Defined Variables and Clearing Variables
Let's say you 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 whos command does. Simply typing whos at the command prompt will return to you the names of all variables that are currently defined. For example:
clear % clear variables from memory - see notes below
5
a = 5
a =
6
b = 6
b =
whos
| Name | Size | Bytes | Class |
| a | 1x1 | 8 | double array |
| b | 1x1 | 8 | double array |
Grand total is 2 elements using 16 bytes
Once you are done with the variables that you have defined how do you remove them from memory? This is exactly what the clear command is for. 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:
whos
| Name | Size | Bytes | Class |
| a | 1x1 | 8 | double array |
| b | 1x1 | 8 | double array |
Grand total is 2 elements using 16 bytes
clear
whos
Suppressing Results with Semicolons
Semicolons typed after commands can be used to hide the printing out of results. If you type an expression (such as "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_blank_average = (a + b + c + d + e) / 5;
the_blank_average
the_blank_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. And by 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.
Variable Names and Assigning Strings to Variables
There are some specific rules for what you can name your variables, so you have to be careful.
- Only use primary alphabetic characters (i.e., "A-Z"), numbers, and the underscore character (i.e., "_") in your variable names.
- You cannot 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).
- 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.
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
265
a*b
ans =
Continue on to the next lesson.
Store