
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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 >>