Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Local Functions in MATLAB
Next article icon

Inline and Anonymous Functions in MATLAB

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
% MATLAB program for inline function. 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
% MATLAB program to draw the  % plot using inline function. 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. 

Matlab
% MATLAB program for perform  % the Anonymous function.  y = @(x)  x^2-1;  % get the x value.  y(2) 

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.

Matlab
% MATLAB program for perform  % the Anonymous function.  y = @(x)  x^2-1;  % get the x value.  y(0) 

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
% MATLAB program for perform the Anonymous function. 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
% MATLAB program for perform the Anonymous function. 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
% MATLAB program for perform the Anonymous function. Anonymous_Function = @(a,b) (a^2 + b^2 + a*b); a = 2; b = 20; x = Anonymous_Function(a,b) 

Output:

 

Next Article
Local Functions in MATLAB
author
sourabhsahu33
Improve
Article Tags :
  • Technical Scripter
  • MATLAB
  • Technical Scripter 2022
  • MATLAB-Functions

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
  • 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
  • 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
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences