Kahibaro
Discord Login Register

Variables and Assignment

Why Variables Matter in MATLAB

In MATLAB, variables are names that refer to stored values. Whenever you calculate something, load data, or type a number and want to use it later, you put it into a variable. MATLAB is designed to work with variables that often represent vectors and matrices, but the idea of a variable starts with simple numbers.

A variable lets you avoid repeating the same value, and it also lets your code adapt to new inputs. Instead of writing the same constant in many places, you assign it to a variable once and then use the variable name everywhere.

Naming Variables

MATLAB variable names must follow specific rules. A valid variable name starts with a letter, and can then contain letters, digits, and underscores. For example, a, result1, and signal_mean are valid names, while 1value or total-cost are not.

MATLAB is case sensitive. This means Data, data, and DATA are three different variable names. Consistent use of capitalization improves readability and helps you avoid hard to find mistakes.

You cannot use MATLAB keywords as variable names. For instance, if, for, while, and end are reserved for the language itself. If you try to assign to one of these, MATLAB will give an error. There are also some function names, such as sum or mean, that you technically can use as variable names, but it is a bad idea, because it will hide the original function while that variable exists.

Meaningful variable names make scripts easier to read. A name like temperatureC explains more than t1. For beginners, using descriptive names is often more valuable than saving a few characters.

Assigning Values with the Equal Sign

In MATLAB, assignment uses the equal sign =. Assignment means that MATLAB evaluates the expression on the right side of =, then stores the resulting value in the variable named on the left side.

For example, you can type in the Command Window:

x = 5
y = 3 + 4
z = x * y

After you run these commands, x stores 5, y stores 7, and z stores 35. The expression on the right can be a single number, a calculation, the result of a function call, or a more complex expression. MATLAB always performs the calculation first, then assigns the result.

You can reassign a variable by using = again. If you write:

x = 10
x = x + 1

then the first line gives x the value 10, and the second line changes x to 11. MATLAB does not remember the old value once it is replaced.

You can also assign the result of a function to a variable:

r = sqrt(9)

In this case, MATLAB evaluates sqrt(9) as 3, and then stores 3 in r. Using assignment like this is standard practice when you want to reuse results, or when you want to make code more understandable by naming intermediate values.

The Special Variable ans

If you type an expression without assigning it to a variable, MATLAB automatically stores the result in a special variable called ans. For instance:

2 + 3

MATLAB displays 5, and also makes ans equal to 5. If you then type:

ans * 10

MATLAB uses the current value of ans. Each time you run an expression with no explicit assignment, ans is updated. This can be handy for quick calculations, but it is usually better to use named variables when you care about the result, so you do not accidentally overwrite it.

Suppressing Output with Semicolons

By default, when you assign a value in the Command Window or in a script, MATLAB displays the result. If you add a semicolon at the end of a line, MATLAB still performs the assignment, but does not print the value.

Compare:

x = 3 + 4
x = 3 + 4;

Both lines give x the value 7. The first line also shows x = 7 in the Command Window. The second line performs the assignment silently. When you write longer scripts or work with large arrays, ending lines with semicolons keeps output manageable and speeds up execution slightly.

If you want to check a variable whose value you previously assigned with a semicolon, you can simply type its name:

x

and MATLAB will display its current value.

Reusing and Overwriting Variables

Once you assign a variable, you can use it in later expressions. MATLAB always uses the current stored value. For example:

a = 2;
b = 3;
c = a + b;
a = 10;
d = a + b;

Here, c is 5, because at that time a was 2 and b was 3. After you change a to 10, d becomes 13. The value stored in c does not change when you modify a, because c already holds its result. Assignment stores values, it does not keep live links to expressions.

You can assign one variable to another:

x = 100;
y = x;

After this, both x and y are 100. Later changes to x do not automatically change y:

x = 200;

Now x is 200, while y is still 100. This behavior is central to understanding how MATLAB stores and uses variables.

Multiple Assignments and Chaining

You can assign to multiple variables in a single line if you use array or function outputs. One simple pattern is to chain assignments:

a = b = 5

This style does not work in MATLAB the way it does in some other languages, because MATLAB requires each assignment to have a single variable name on the left side. To give a and b the same value, you must write:

b = 5;
a = b;

A more MATLAB specific way to assign several values is to use functions that return multiple outputs, which is covered separately when you learn about function inputs and outputs.

Basic Display and Reassignment in Practice

You can test variable assignment interactively in the Command Window. For example, try entering:

length_cm = 25;
length_in = length_cm / 2.54;
length_cm = 30;
length_in

The first two lines compute length_in from length_cm. Changing length_cm later does not retroactively update length_in, which still has the old result. This shows that each variable holds its own stored value until you explicitly reassign it.

You can also combine assignment and display by omitting the semicolon when you want to see intermediate steps, then reintroduce the semicolon in scripts once you are confident the code behaves as intended.

Variable Names, Functions, and Accidental Shadowing

It is possible to assign a variable with the same name as an existing MATLAB function. For example:

mean = 10;

From this point in your session, when you type mean without arguments, MATLAB treats it as a variable, not a function. If you attempt to call mean as a function, you may get errors or unexpected results. This situation is called shadowing, because the variable hides the function with the same name.

To avoid confusion, try not to reuse common function names for variables. If you accidentally shadow a function, you can remove the variable and restore normal behavior by clearing it in the workspace, which you will learn to manage in later chapters.

Important points to remember:
Variable names must start with a letter and can contain letters, digits, and underscores. MATLAB is case sensitive and keywords cannot be used as variable names.
Assignment uses =. MATLAB always evaluates the right side first, then stores the result in the variable on the left side.
If you do not assign a result to a variable, MATLAB stores it in ans. This value is overwritten every time you run a new unassigned expression.
End a line with ; to suppress output. The assignment still happens, but the value is not printed.
Reassigning a variable replaces its old value. Other variables that were computed earlier do not change automatically.
Avoid using names of built-in functions as variable names, or you may accidentally shadow those functions.

Views: 4

Comments

Please login to add a comment.

Don't have an account? Register now!