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:
Defining Function Handles in MATLAB
Next article icon

Creating Function in Files in MATLAB

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

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 use it again and again in the program i.e. it makes a block of code reusable. It makes it easier to find errors and also reduces code redundancy.

Types of Function:

  1. Library Functions: In MATLAB Software, there are some inbuilt functions, which take input as arguments and return output. E.g. log(x), sqrt(x), line space(a,b,n), etc.
  2. User-Defined Functions: In MATLAB, we also create functions by writing Matlab commands in files that take input/s as argument/s and then return the output. Also, save the function file using the name of the function itself. 

Steps to Create a Function File in MATLAB:

  1. Step 1: Create a file and write commands using the function keyword.
  2. Step 2: Save the function file with the same name as the function. Here I have saved the file with the name solve.m.
  3. Step 3: Use the function in Command Video by providing suitable argument/s.

Now we see creating a function and saving it in MATLAB.

Example 1:

Matlab

% function to calculate sin(x)+cos(x) where x is in radians
% Here solve is the name of the function which
% will return x and takes an argument a
function x = solve(a)
x = sin(a) + cos(a);
                      
                       

Using the function to be called in Command Window.

Example 2:

Matlab

input = 1.5708;
  
% display the function's output
disp(solve(input));
                      
                       

Output:

 

The above screenshot is showing:

  • Function saved as a file named solve.m
  • Command Window in which we are calling function for various inputs.


Next Article
Defining Function Handles in MATLAB

A

akashish__
Improve
Article Tags :
  • MATLAB
  • MATLAB-Functions

Similar Reads

  • How to create a function in MATLAB ?
    A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
    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
  • 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
  • User defined function in MATLAB
    Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
    2 min read
  • 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
  • Inline and Anonymous 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 that is invoked and executed when it is called by the user.  Inline FunctionInline functions are the kind of functions which is defined in one line. that's why this type of fu
    4 min read
  • Alternatives To Eval Function in MATLAB
    The eval function is one of MATLAB's most powerful and flexible commands. Eval is an acronym for evaluating, which is exactly what it does: it evaluates MATLAB expressions. Any command that can be executed from the MATLAB prompt may be executed from a Mfile using eval. Use of Eval Function:The Eval
    3 min read
  • Clean Up When Functions Complete in MATLAB
    While working in MATLAB, handling the workspaces is crucial for the proper working of multiple scripts and functions. That is why it is a healthy habit to clean up the MATLAB workspace once a function execution is completed. Some of the requirements for performing a cleanup routine, cleaning up all
    4 min read
  • Map Function in MATLAB
    A map function basically takes the elements of the array and applies a function to each element.  The resultant output is of the same shape as the array, but the values are the result of the function. In MATLAB, there is a similar function called arrayfun(), which we can be used to achieve the same
    2 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