
- 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 - Anonymous Functions
A MATLAB function is a separate, self-contained block of code that performs a specific task and can accept input arguments and return output values. Functions help in organizing your code, making it more readable, and promoting code reuse.
Creating a Simple MATLAB Function
Suppose we want to create a function that calculates the square of a given number. Here's how you can do it −
Step 1 − Create a New MATLAB Script
Open MATLAB and create a new script file by clicking on the "New Script" button or using the edit command. Save it with a .m file extension (e.g., square.m).
Step 2 − Define the Function
Inside the square.m file, define the function as follows −
function result = square(x) % This function calculates the square of a number. result = x^2; end
In matlab it will be as follows −

In this code −
- function result = square(x) declares the function named square that takes one input argument x and returns a result.
- result = x^2; calculates the square of the input x and assigns it to the variable result.
Step 3 − Use the Function
Now that you've created the function, you can use it in your MATLAB workspace or other scripts. Here's how you can use it −
>> num = 5; result = square(num) result = 25 >>
Now that we know how to create normal functions in Matlab, let us get a more detailed understanding of Anonymous functions in matlab.
What are Anonymous Functions?
Anonymous functions are small, unnamed functions in MATLAB that are defined using the @(inputs) expression syntax. They are also known as lambda functions because they allow you to create functions without explicitly naming them. Anonymous functions can have multiple inputs and can return a single output.
Creating an Anonymous Function
To create an anonymous function in MATLAB, you use the @(inputs) expression syntax. Here's a simple example −
square = @(x) x^2;
In this example, we define an anonymous function called square that takes one input x and returns its square.
Using Anonymous Functions
Anonymous functions can be used just like regular functions. You can call them with arguments, assign them to variables, and pass them as arguments to other functions. Here are some examples −
result = square(5); % Calls the anonymous function with x=5, result is 25.
Matlab Anonymous Functions Usage
Anonymous functions with no variables, also known as constant anonymous functions, are used especially in situations where you need to pass a function to another function but don't require any input arguments.
You can create a constant anonymous function with no input arguments by using the @() expression syntax. Here's an example
constant_func = @() 42;
In this example, we define an anonymous function called constant_func that takes no input arguments and always returns the constant value 42.
Anonymous functions of no variable also called constant anonymous function
Constant anonymous functions can be useful as function handles to other functions that expect a function argument. Here's an example −
function applyFunctionToArray(arr, func) result = func(); % Call the function with no arguments disp(result); end % Use the constant anonymous function as an argument constant_func = @() 42; applyFunctionToArray([1, 2, 3], constant_func);
In this example −
- We define a custom function applyFunctionToArray that takes an array arr and a function handle func as arguments.
- Inside applyFunctionToArray, we call the provided function func with no arguments, as it doesn't require any.
- We use the anonymous function constant_func as an argument to applyFunctionToArray. It will always return 42.
- When we call applyFunctionToArray with an array [1, 2, 3] and the constant anonymous function, it prints 42 to the console.
Anonymous Functions with one Variable
You can create an anonymous function with one variable in MATLAB using the @(variable) expression syntax. Here's an example −
square = @(x) x^2;
In this example, we define an anonymous function called square that takes one input variable x and returns the square of x.
You can call the function as follows −
result = square(5);% Calls the anonymous function with x=5, result is 25.
Anonymous Functions with Multiple Variables
Anonymous functions with multiple variables in MATLAB are used when you want to define a simple, unnamed function that takes more than one input argument.
To create an anonymous function with multiple variables, you can use the @(var1, var2, ...) expression syntax. Here's an example −
addition = @(x, y) x + y;
In this example, we define an anonymous function called addition that takes two input variables, x and y, and returns their sum.
You can call the anonymous function as shown below −
result1 = addition(4, 5); % the result1 = 9
Anonymous Functions as Arguments to other Functions
In the example below I am going to make use of the arrayfun function, which applies a given function to each element of an array.
% Define an array of numbers numbers = [1, 2, 3, 4, 5]; % Use arrayfun to double each element using an anonymous function doubled_numbers = arrayfun(@(x) 2 * x, numbers)
In this example −
- An array of numbers called numbers is defined with values [1,2,3,4,5].
- We use the arrayfun function and provide an anonymous function @(x) 2 * x as the first argument. This anonymous function takes one input x and returns 2 * x, effectively doubling the value of x.
- arrayfun applies this anonymous function to each element of the numbers array, resulting in a new array called doubled_numbers.
- Finally, we display the doubled_numbers array, which contains the doubled values of the original array.
When you execute the same in matlab the output is −
>> numbers = [1, 2, 3, 4, 5]; doubled_numbers = arrayfun(@(x) 2 * x, numbers) doubled_numbers = 2 4 6 8 10