VARIABLES
In MATLAB, every variable is a matrix form or 2D array form. In MATLAB assignment of variables is very simple.
For example-1
When we assign the value of x with 4 in MATLAB, it will show something
like this:-
>> x=4 %using assignment operator x
value is defined
|
MATLAB will run and execute the above statement and give result:-
x =
4
|
It creates a 1*1 matrix named x storing value of 4
For example-2
Let us do the square root of a number
>>x=sqrt(25) %using assignment operator x value is
defined
|
MATLAB will run and execute the above statement and give result:-
x =
5
|
Note −
- Once a variable is entered into the system, you can refer to it later till Workspace Window having the previously stored value
- Variables must assigned with values before they are used.
- When an expression returns a result that is not assigned to any variable, to use that unassigned value in other cases just use ans value with the statement. It will be easy to understand by an example.
For example-3
Let us add two numbers and multiply the output with another number
>>x=2;
>>y=3;
>>a=x+y;
|
MATLAB will run and execute the above statement and give result:-
a=
5
|
>>z=4;
>>a*z
|
MATLAB will run and execute the above statement and give result:-
ans=
20
|
The who command displays all the variable names in workspace
window or MATLAB project you have used.
For example:-4
I have used three variables such as a,b,c, now I use who in command
prompt:-
>>who
|
MATLAB will run and execute the above statement and give result:-
Your variables are:
a b
c
|
The whos command displays a full description about the variables. Such
as:-
It shows the size, memory allocated for the variable, class of the
variable and attributes of the variable
>>whos
|
MATLAB will run and execute the above statement and give result:-
Name Size Bytes Class Attributes
a
1x1 8 double
b
1x1 8 double
c
1x1 8 double
|
clear:-
The command clear removes all the variables from the system.
For example:-5
>>a=2
a =
2
>> b=4
b =
4
>> c=3
c =
3
>> whos
Name
Size Bytes Class
Attributes
a
1x1 8 double
b
1x1 8 double
c
1x1 8 double
|
total is 3 elements using 24
>>clear a
>> whos
Name
Size Bytes Class
Attributes
b
1x1 8 double
c
1x1 8 double
|
total is 2 elements using 16
>> clear
>> whos
|
We selectively removed all variables by using the command clear a. after typing whos we find that a is no longer
a variable.
clear
all:-
the clear all
command removes all variables at a time.
For example:-6
>>a=2
a =
2
>> b=4
b =
4
>> c=3
c =
3
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 8 double
c 1x1 8 double
|
total is 3 elements using 24
>>clear a
>> whos
Name Size Bytes Class Attributes
b 1x1 8 double
c 1x1 8 double
|
total is 2 elements using 16
>>clear all
>> whos
|
Note:-
- clear and clear all command is used full in removing variable
from the memory and free the allocated memory.
OPERATORS
An operator can be
defined as which operates an operation between two or more variables.
In MATLAB, there are
some common operators are used. Which are listed below:-
Operator
|
Purpose
|
+
|
Plus; addition operator.
|
-
|
Minus; subtraction operator.
|
*
|
Scalar and matrix multiplication operator.
|
.*
|
Array multiplication operator or vector multiplication operator.
|
^
|
Scalar and matrix exponentiation operator.
|
.^
|
Array exponentiation operator. Or vector exponentiation operator.
|
\
|
Left-division operator.
|
/
|
Right-division operator.
|
.\
|
Array left-division operator or vector
left-division operator.
|
./
|
Array right-division operator or vector right-division
operator.
|
:
|
Colon; generates equispaced elements
and represents an entire
|
()
|
Parentheses; encloses function arguments and
array indices;
|
[ ]
|
Brackets; enclosures array elements.
|
.
|
Decimal point.
|
…
|
Ellipsis; line-continuation operator
|
,
|
Comma; separates statements and elements in
a row
|
;
|
Semicolon; separates columns and suppresses
display.
|
%
|
Per cent sign; designates a comment and specifies formatting.
|
_
|
Quote sign and transpose operator.
|
._
|
Nonconjugated transpose operator.
|
=
|
Assignment operator.
|
SPECIAL VARIABLES
MATLAB supports some special variables and constants.
Name | Meaning |
ans
|
Most recent answer
|
eps
|
Accuracy of floating-point precision
|
i,j
|
The imaginary unit √-1
|
inf
|
Infinity
|
NaN
|
Undefined numerical result not a number
|
pi
|
The number of
|
Note:-
· This special variables need not to be memorized, because these are used frequently.
· Not a number (NaN):- in computing, NaN, sands for Not a Number, is a member of a
numeric data type that can be undefined mostly in floating-point arithmetic.
Naming Variables
- Variables names consist of a letter followed by any number of letter, digits or underscore.
- MATLAB is a case-sensitive language
- Variable names can be of any length, however, MATLAB uses only characters, where character name is given by the function <namelengthmax>
- Variable names should not be named as any pre-defined function names and it should not be started from any number or special character.
- If you want to create a variable which is having more than one word and you want to make them separate do not use space.
Name of the variable= my prog file.m (wrong)
Name of the variable= myprogfile.m (right)
Name of the variable= my_prog_file.m (right)
Post a Comment
If you have any doubts regarding this topic...
feel free to ask...