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;

>>a^b

ans =

32

>>x = 5/2*pi;

>>y = sin(x)

y =

1

>>z = asin(y)

z =

1.5708

>>who

>>whos

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.

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

>>a=5;

>>b=10;

>>c=a*b

c=

50

>>clc % observe in command window

>>clear a

>>clear all % observe in workspace

**Again in command window**

>>t=0:0.01:100;

>>figure(1)

>>plot(t,sin(t))

>>figure(2)

>>plot(t,tan(t))

>>figure(3)

>>plot(t,e^t)

>>close figure(2) observe

>>close all observe

** PRACTICE**

>> % Define a

>> a = 2

a =

2

>> % Define b

>> b = 5

b =

5

>> % Compute c

>> c = a + b^2

c =

27

>> % Compute a different value for c

>> c = a + 2 * b ;

>> % Display c

>> c

c =

12

>> clear % Clear all variables

>> c

??? Undefined function or variable 'c'

** YOUR TURN**

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.

Translate »