# 4. Variables

In this lesson you will learn:

- MATLAB variables.
- Multiple assignment.
- Who, Whos and Clear commands.
- Assigning the ellipses.
- The format command.
- Creating vectors.
- Creating matrices.

**1. MATLAB variables:**

In MATLAB environment, every variable is an array or matrix. You can assign variables in a simple way. For example, x = 3 ( defining x and initializing it with a value ). It creates a 1-by-1 matrix named x and stores the value 3 in its element.

**Note that:**

- Once a variable is entered into the system, you can refer to it later.
- Variables must have values before they are used.
- When an expression returns a result that is not assigned to any variable, the system assigns it to a variable named ans, which can be used later. For example,

Now try it yourself

x = 7 * 8; y = x * 7.89

What will be value of y?

**2. Multiple assignment:**

You can assign multiple variables on the same line. For example, a = 2; b = 7; c = a * b MATLAB will execute it as shown in figure.

**3. Who, Whos and Clear commands:**

**Who:**

The who command displays all the variable names you have used.

**Whos:**

The whos command displays little more about the variables:

- Variables currently in memory.
- Type of each variables.
- Memory allocated to each variable.
- Whether they are complex variables or not

**Clear:**

The clear command deletes all or the specific variable(s) from the memory.

- clear x ( it will delete x variable, won’t display anything )
- clear ( it will delete all variables in the workspace peacefully and unobtrusively)
- clc ( it will clear command window )

**4. Assigning the ellipses:**

If a statement is very long then it can be extended to other line by using the command called ellipses ( … ). For example,copy the following example and check it on MATLAB

number_1=20; number_2=40; average=(number_1 ... + number_2)/2

**5. The format commands:**

By default, MATLAB displays numbers with four decimal place values. This is known as format short. However, if you want more precision, you need to use the format command. The format long command displays 16 digits after decimal. Both **format long** and **format short** is shown in figure below.

**Format bank:**

The format bank command rounds numbers to two decimal places.

**Format short e:**

The format short e command allows displaying in exponential form with four decimal places plus the exponent as shown.

**Format long e:**

The format long e command allows displaying in exponential form with 15 decimal places plus the exponent as shown.

**Format rat:**

The format rat command gives the closest rational expression resulting from a calculation as shown.

**6. Creating vectors:**

A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors:

- Row vectors
- Column vectors

**Row vectors** are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements as shown in example 1 and example 2.

**Column vectors** are created by enclosing the set of elements in square brackets, using semicolon ( ; ) to delimit the elements as shown.

**7. Creating matrices:**

A matrix is a two-dimensional array of numbers. In MATLAB, a matrix is created by entering each row as a sequence of space or comma separated elements, and end of a row is demarcated by a semicolon. For example, let us create a 3-by-3 matrix as:

So this is the end of our fourth lesson. Please share this article if you like and do not forget to comment.