Inline and Anonymous Functions in MATLAB
Last Updated :
26 Apr, 2025
Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code that is invoked and executed when it is called by the user.
Inline Function
Inline functions are the kind of functions which is defined in one line. that’s why this type of function is called the one-linear function. Inline functions are classified into two categories:
- In-Built Inline function
- User Defined Inline function
So let’s take some examples to understand the inline functions. For the inline function we have to create a new script file then write the code inside that file and run that code.
Example 1:
Matlab
clc;
close all;
Inline_function = @(x,y) (x^2 + y^2);
result = Inline_function(2,4)
|
Output:
In this example, we will represent the graphical(plot), figure. using the inline function. so here we create a function and take some variables that help to make a plot in MATLAB.
Example 2:
Matlab
clc;
close all;
Anonymous_Function = @(d,t,w) (exp(d*t).* sin(w*t));
time = linspace(0,2,201);
frequency = 100;
omega = 2 * pi * frequency;
digit = -1;
P = Anonymous_Function(digit, time, omega);
plot(time,P)
|
Output:
Anonymous Function
An Anonymous function is a kind of MATLAB function that is not stored program file location. or an Anonymous function is a single line of executable expression function that can accept multiple inputs and return one output.
An anonymous function can contain a single-line executable statement. these functions are not stored in program files, but it is connected with a data type function_handle (@).
Use of Anonymous Function
Example 1:
- Suppose we have an equation
- y = x2 – 1
So we could write this equation as an anonymous function in MATLAB with the following line of code. But remember the Anonymous function don’t need any new script file, directly we can write and execute in the command window.
In the above code, we y=@(x), where the @ is identified as a function handle what it does In MATLAB it’s a data type that stores an association to a function.
Output:
As we know that for the Anonymous function we don’t need any program file we can execute this program in Command Window in MATLAB, as you can see in the below image.
If you will change just one line in the above code you will get a different output.
Output:
Let’s look into another Example –
Example 2:
Suppose in this example I want to perform the Anonymous function that takes just a single line of command for execution. So, here we are defining the function name as cube and defining the x variable.
Matlab
Cube = @(x) x.^3;
Cube(15)
|
Using the above command line we will find the cube of any number just using the one-line Anonymous function.
Output:
And also if we want to find the cube of others’ numbers Simultaneously then we have to give the command as following and then hit enter.
Matlab
Cube = @(x) x.^3;
x = [3 5 7]
x =
3 5 7
Cube(x)
|
Output:
Example 3:
Anonymous function with multiple input and output-
Matlab
Anonymous_Function = @(a,b) (a^2 + b^2 + a*b);
a = 2;
b = 20;
x = Anonymous_Function(a,b)
|
Output:

Similar Reads
Anonymous Functions in MATLAB
A block of code that is organized in such a way that is reusable for the entire program. Functions are used for reducing efforts made by writing code and making the program short, and easily understandable. There are different syntaxes for declaring a function in different programming languages. In
5 min read
Inline Functions in MATLAB
Inline functions are those functions that are defined in one line, also called one-liner for the same reason. In MATLAB there are two types of inline functions, inbuilt and user-defined. Let's take a look at both of them. InBuilt inline Functions:MATLAB has many mathematical functions built-in which
2 min read
Creating Function in Files in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Functions:The function is a set of statements or commands, which take input/s as parameters and return output. We write functions to
2 min read
Find() function in MATLAB
The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
Scripts and Functions in MATLAB
In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
Local Functions in MATLAB
Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a function:Before moving forward let's see how actually t
2 min read
Defining Function Handles in MATLAB
A MATLAB data type that represents a function is called a function handle, in other words, we say that The function handle is a typical data type in MATLAB. Function handles can therefore be modified and used in the same way as other MATLAB data types. Using function handles in arrays, structures, a
3 min read
Functions in MATLAB
Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code which is invoked and executed when it is called by the user. It contains local workspace and independent of base workspace which belongs to command prompt. Let's take a glance
5 min read
Recursive Anonymous Function in Golang
Recursion is a process in which a function calls itself implicitly or explicitly and the corresponding function is called recursive function. Go language supports special feature called anonymous function. It is a function that doesnât contain any name. It is used to create an inline function. Recur
2 min read
Overloading Functions in Class Definitions in MATLAB
The overloading Function is like in all other programming languages the definition of Overloading functions. It is basically defined as more than one function with the same name but different parameters and data types. In other words, function overloading is the feature of Object Oriented Programmin
3 min read