- Contact us
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.
• 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.
• Variable = number; (x=123;)
• Variable = expression; (x=y+123;)
• 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
• 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;
>>x = 5/2*pi;
>>y = sin(x)
>>z = asin(y)
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.
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.
This command clears the command window but all the variables are present in the workspace
clear or clear all
>> clear (or)
This command permanently clears from the workspace and not availed for further use.
>> clear x % clears variable x permanently from workspace.
Closes all the figure windows in the MATLAB
Closes figure 3 only.
Note: If you use close, the last / highlighted figure is closed.
In command window
>>clc % observe in command window
>>clear all % observe in workspace
Again in command window
>>close figure(2) observe
>>close all observe
>> % Define a
>> a = 2
>> % Define b
>> b = 5
>> % Compute c
>> c = a + b^2
>> % Compute a different value for c
>> c = a + 2 * b ;
>> % Display c
>> clear % Clear all variables
??? 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.