
- 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 - Function Handles
Function handles in MATLAB are a powerful tool that allows you to treat functions as variables. They provide a way to store, pass, and manipulate functions in your code. Function handles are particularly useful when you need to work with functions as arguments to other functions, create custom functions on-the-fly, or when you want to switch between different functions dynamically.
What is a Function Handle?
A function handle in MATLAB is a data type designed to store a reference to a function. Using a function handle allows you to invoke the function indirectly, regardless of where it's called in your code.
Common applications of function handles include −
- Passing a function as an argument to another function, often referred to as "function functions." For instance, you can pass a function to integration and optimization functions like integral and fzero.
- Specifying callback functions, such as those used to respond to UI events or interact with data acquisition hardware.
- Creating handles to functions defined inline, rather than having them stored in a separate program file, as is the case with anonymous functions.
- Enabling the invocation of local functions from outside the main function.
Creating Function Handles
Creating function handles in MATLAB is a fundamental concept that allows you to store references to functions for later use. This is particularly useful when you want to pass functions as arguments, work with anonymous functions, or dynamically switch between different functions in your code.
To create a function handle, you can use the @ symbol followed by the function name or an anonymous function. Here's how you can do it −
Using Named Functions
% Define a named function function result = myFunction(x) result = x^2; end % Create a function handle to myFunction funcHandle = @myFunction;
In MATLAB, you can define a named function using the function keyword. This function, myFunction, takes one input argument x and returns the square of x. Here's the code for this named function −
% Define a named function function result = myFunction(x) result = x^2; end
In this example, myFunction calculates the square of the input x and assigns it to the variable result.
In matlab the function myFunction will be as follows −

Now I will create a function handle. To create a function handle to myFunction, you use the @ symbol followed by the function name −
% Create a function handle to myFunction funcHandle = @myFunction;
Here, funcHandle is a variable that now stores a reference to the myFunction function. It can be used to call myFunction indirectly.
The function handle is created in matlab command window as shown below −

If you see the workspace the funcHandle is created in it and the class of it is function_handle.
Let us now understand how to make use of these Function handle.
Using the Function Handle
Now that you have a function handle, you can use it to call the myFunction function. For example −
result = funcHandle(5);
In this code, funcHandle(5) calls myFunction with x = 5, and the result is stored in the variable result. In this case, the result will be assigned the value 25 because myFunction squares the input.
When you execute in matlab command window the output is −
>> funcHandle = @myFunction; >> result = funcHandle(5) result = 25 >>
Using Anonymous Functions
Anonymous functions are often more convenient when you need to create simple, short functions quickly −
% Create an anonymous function to square a number funcHandle = @(x) x^2;
You can call the function handle as follows −
result = funcHandle(5)
The execution in matlab command window is −
>> funcHandle = @(x) x^2; >> result = funcHandle(5) result = 25 >>
Pass Function Handles as Arguments
Function handles are commonly used as arguments in other functions, especially when you want to apply the same operation to different data −
% Define a custom function that takes a function handle as an argument function result = applyOperation(func, a, b) result = func(a, b); end % Use applyOperation to apply different operations add = @(a, b) a + b; subtract = @(a, b) a - b; result1 = applyOperation(add, 5, 3); % result1 = 8 result2 = applyOperation(subtract, 5, 3); % result2 = 2
Let us understand the above code in detail.
Defining Custom Function
The custom function let us create the same in matlab as shown below −

- In the above function applyOperation, it takes an argument func, which is a function handle.
- func is a function handle that represents an operation (e.g., addition or subtraction).
- a and b are the input values on which the operation represented by func will be performed.
- result is the output of applying the operation specified by func to a and b.
Define Function Handles for Different Operations
Two function handles, add and subtract, are defined to represent addition and subtraction operations, respectively −
add = @(a, b) a + b; subtract = @(a, b) a - b;
- add is a function handle that takes two inputs a and b and returns their sum.
- subtract is a function handle that takes two inputs a and b and returns their difference.
Use applyOperation to Apply Different Operations
The applyOperation function can be used to apply different operations to input values.
Using add function handle
result1 = applyOperation(add, 5, 3);
Using subtract function handle
result2 = applyOperation(subtract, 5, 3);
Executing the above code in matlab command window the output is −
>> add = @(a, b) a + b; subtract = @(a, b) a - b; >> result1 = applyOperation(add, 5, 3) result1 = 8 >> result2 = applyOperation(subtract, 5, 3) result2 = 2