Home » All » Variables Declaration in MATLAB

Variables Declaration in MATLAB

Variables Declaration in MATLAB

In this section, Variables Declaration in MATLAB , assignment of variables, special variables, commands involving in variables etc were discussed. Variables and constants are integral part of a program. First, we need to know “what is Variable”? Variables are the numerical values assigned are subject to change during execution of a program. Whereas Constants refer to a fixed value, that doesn't change during execution time of program.

Subscribe to Our YouTube Channel by Clicking ( HERE ) For  FREE Video Courses

Variable names:

•    Must start with a letter (x, y, MyVar,…)
•    May contain only letters, digits, and the underscore “_”.
•    MATLAB is case sensitive, for example one & ONE are different variables.
•    MATLAB only recognizes the first 31/63 characters in a variable name.

Assignment statement

•    Variable = number; (x=123;)
•    Variable = expression; (x=y+123;)

Special variables

•    ans: default variable name for the result.
•    pi: π = 3.1415926 ……
•    eps: ε= 2.2204e-016, smallest value by which two numbers can differ
•    inf: ∞, infinity
•    NAN or nan: not-a-number
•    i and j: imaginary number
•    realmax     largest floating point number
•    reammin     smallest positive floating point number
•    nargin     number of function input arguments
•    nargout     number of function output arguments
•    computer     computer type

Commands involving variables

•     who: lists the names of the defined variables
•     whos: lists the names and sizes of defined variables
•    clear: clears all variables
•    clear varname: clears the variable name
•    clc: clears the command window
•    clf: clears the current figure and the graph window
>>a = 2;
>>b = 5;
ans =
>>x = 5/2*pi;
>>y = sin(x)
y =
>>z = asin(y)
z =

The percentage symbol (%)

When this is used in command window before a command or text, this total line detects as comment and it will not display. Normally it uses in script/ M-file for the commenting of your program.

Note: If   %% is uses at the stating of the line, then it becomes a cell
% used in fprintf command where the value to be displayed.

Subscribe to Our YouTube Channel by Clicking ( HERE ) For  FREE Video Courses

The semicolon ( ; )

If it is typed/ used at the end of the command, the output is not displayed in the command window. This is used when the variable value is known/ oblivious  or length is very big.
Note: In matrices, this is used as end of the row.

Some general commands for command window
clc- clear the command window.
>>clc  (enter)
This command clears the command window but all the variables are present in the workspace
clear  or clear all
>> clear   (or)
>>clear all
This command permanently clears from the workspace and not availed for further use.
clear variable
>> clear x % clears variable x permanently from workspace.
close all
>>closes all
Closes all the figure windows in the MATLAB
close figure(n)
>>close figure(3)
Closes figure 3 only.
Note: If you use close, the last / highlighted figure is closed.

In command window
>>clc   % observe in command window
>>clear  a
>>clear all  % observe in workspace
Again in command window
>>close figure(2)      observe
>>close all                observe

>> % Define a
>> a = 2
a =
>> % Define b
>> b = 5
b =
>> % Compute c
>> c = a + b^2
c =
>> % Compute a different value for c
>> c = a + 2 * b  ;
>> % Display c
>> c
c =
>> clear % Clear all variables
>> c
??? Undefined function or variable 'c'

Set variable a to 12345679, variable b to 9, and assign your favorite number between 1 and 9 to c. Compute the product abc and display it on the screen. Clear all variables at the end and observe.