MATLAB - Predefined Variables



Predefined variables are variables that are already defined in a programming language.They will already have a value when you try to print it. They are not reserved as keywords and can be used in your program.If used directly these variables will have some value, but can be overwritten and can be used as variable names in your code.

It is a good programming practice to not use the pre-defined variables as variable names in your code as it can give unexpected results.

Predefined Variables in MATLAB

Here is a list of predefined variables available in Matlab.

Variable Description
pi Here pi variable will give the value for Π up to 15 significant digits.
i,j They are variables for complex numbers.
inf Shows infinity values.An example is a result for division by zero.
NaN It is Not a number , which is the result of some unwanted return from a function.
clock This will give you current time, in vector format that will represent year,month,day,hour,min,second.
date A string value with today;s date.
eps It holds the smallest value called an epsilon.
ans This is noticed when you use the command line matlab tool. The result is stored in ans and the variable is available in matlab workspace.
whos List the variables in matlab
global Declares given variable as global

Let us understand with an example each of the predefined variables in Matlab.

pi

Here is an example of a pi in Matlab.

>> pi

ans =

    3.1416

>> 

As mentioned earlier predefined variables are not reserved keywords and can be overwritten as shown below.

>> pi = 2

pi =

     2

>> 

I,j: Complex Numbers

Here is the example when you check the values of i and j in Matlab.

>> i

ans =

   0.0000 + 1.0000i

>>
>> j

ans =

   0.0000 + 1.0000i

>> 

An example where the value of j is changed to 12.

>> j = 12

j =

    12

>> i+j

ans =

  12.0000 + 1.0000i

>> 

Example of complex numbers i

x = [1:3]';
y = [5:-3:3]';

z = x+i*y

On execution in matlab you will get

>> x = [1:3]';
y = [5:-3:3]';

z = x+i*y

z =

   1 + 5i
   2 + 5i
   3 + 5i

>> 

Example of complex numbers i

x = [1:3]';
y = [5:-3:3]';

z = x+j*y

On execution in matlab you will get

>> x = [1:3]';
y = [5:-3:3]';

z = x+j*y

z =

   1 + 5i
   2 + 5i
   3 + 5i

>> 

Inf: Infinity Value

Here is an example

>> inf

ans =

   Inf

>> 

You can also create array of inf values using Inf as shown in the example below

X = Inf(3)

On execution you will get

>> X = Inf(3)

X =

   Inf   Inf   Inf
   Inf   Inf   Inf
   Inf   Inf   Inf

>> 

NaN: Not a Number

The values that are not real or complex numbers with special value is called NaN , it stands for Not a Number. For expressions like 0/0, inf/inf will give you the result as NaN.

Here is an example of a NaN in Matlab.

>> NaN

ans =

   NaN

>>

In this example will create array of NaNs as shown below

X = NaN(3)

On execution in Matlab you will get

>> X = NaN(3)

X =

   NaN   NaN   NaN
   NaN   NaN   NaN
   NaN   NaN   NaN

>> 

Here we are going to create a 2-by-3-by-4 array of NaN values and later display its size.

X = NaN(2,3,4);
size(X)

On execution in MAtlab you will get

>> X = NaN(2,3,4);
size(X)

ans =

     2     3     4

>> 

Clock: Gives Current Date and Time

The clock will return the date vector with [year month day hour minute seconds].

Here is an example of a clock in Matlab.

>> clock

ans =

   1.0e+03 *

    2.0230    0.0040    0.0300    0.0100    0.0400    0.0434

>> 

You can also use the clock as a normal variable and overwrite it.

>> clock =1

clock =

     1

>> 

Date: Returns Todays Date

Here is an example for date in Matlab.

>> date

ans =

    '30-Apr-2023'

>> 

You can also overwrite the value as shown below.

>> date = "today"

date = 

    "today"

>> 

eps: Epsilon Value

Here is an example

>> eps

ans =

   2.2204e-16

>> 

You can also overwrite the value as shown below.

>> eps = 11

eps =

    11

>> 

ans: Result Stored

Here is an example

>> 1+1

ans =

     2

>>

You can use ans as normal variable name

>> ans = 10

ans =

    10

>> 

whos

This command will list you all the variables with its size and type in workspace.

Example

>> whos
  Name      Size            Bytes  Class     Attributes

  A         3x3                72  double              
  B         4x4               128  double              
  a         1x1                 8  double              

>> 

Global

This will make the variable global.

Syntax

global  var1 var2   varN

This will make var1 var2 varN global in scope.

Here is an example of global variable

global test
test = 10

On execution in Matlab you will get following

>> global test
>> test = 10

test =

    10

>> whos
  Name      Size            Bytes  Class     Attributes

  A         3x3                72  double              
  B         4x4               128  double              
  C         1x1                 8  double              
  a         1x1                 8  double              
  test      1x1                 8  double    global    

>>
Advertisements