4. Variables

In this lesson you will learn:

  1. MATLAB variables.
  2. Multiple assignment.
  3. Who, Whos and Clear commands.
  4. Assigning the ellipses.
  5. The format command.
  6. Creating vectors.
  7. 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,

Variable 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.

Multiple assignment

3. Who, Whos and Clear commands:

  • Who:

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

Who operator

  • Whos:

The whos command displays little more about the variables:

  1. Variables currently in memory. 
  2. Type of each variables.
  3. Memory allocated to each variable.
  4. Whether they are complex variables or not

Whos operator

  • Clear:

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

  1. clear x ( it will delete x variable, won’t display anything )
  2. clear ( it will delete all variables in the workspace peacefully and unobtrusively)
  3. 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

Ellipses command

 

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 long and short

  • Format bank:

The format bank command rounds numbers to two decimal places.

Format bank

  • Format short e:

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

Format short e

  • Format long e:

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

Format long e

  • Format rat:

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

Format rat

 

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.

Row vector

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

Column vector

 

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:

Matrices

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

Note: To ask question you need take this course first and please fill both queston and comment section below. Otherwise your question will not be display. Thank you

Comment

Questions