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 )
MATLAB will execute :
 x =
        3
It creates a 1-by-1 matrix named x and stores the value 3 in its element.

>> Note that: 


1. Once a variable is entered into the system, you can refer to it later.

2. Variables must have values before they are used.
3. 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 ?

Multiple Assignments:


You can assign multiple variables on the same line. For example,

a = 2; b = 7; c = a * b
MATLAB will execute it as
c = 14   as shown



Who, Whos and Clear commands:


>> who

The who command displays all the variable names you have used. 
who 
MATLAB will show all the variables you used and show:



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



>> clear

The clear command deletes all or the specific variable(s) from the memory.
1. clear x   ( it will delete x, won't display anything )
2. clear      ( it will delete all variables in the workspace  peacefully and unobtrusively)
3. clc         ( it will clear command window )

Assigning the ellipses:


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


number_1=20;

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



The format Command:


>> 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 short is shown in figure below.




>> The format bank command rounds numbers to two decimal places. For example,




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




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




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




Creating vectors:


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

1. Row vectors 
2. 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.




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: