EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Matlab Tutorial MATLAB Functions

MATLAB Functions

Priya Pedamkar
Article byPriya Pedamkar

Updated March 21, 2023

MATLAB Functions

Introduction to MATLAB Functions

MATLAB Functions are written with various lines of code that relate one variable with another variable, and each output is related exactly to one particular input that forms an important part of any programming language. In MATLAB environment, they are stored in a certain file like script files, etc. They can accept more than one input argument and can also return more than one output argument. They operate on the defined variables in their predefined workspace, known as the local workspace.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Types of Functions in MATLAB

In Matlab, functions can be created or defined  using the below syntax:

function [o1,o2, ..., oN] = myfun(i1,i2,i3, ..., iN)

 Here my fun is the function name that accepts the input arguments i1,i2..iN and returns output o1,o2…on. The above statement should be first-line while declaring the function. There are some rules which need to be followed for valid function names and saving the function:

  • The function name, which begins with alphabets and can contain number, characters or underscores, are considered valid.
  • We can save the function in a function file that contains function definitions, and the name of the file must match the first function name in the file.
  • We can also save the function, which contains function definitions and commands. Functions should be present at the end of the file, and the script file name cannot have the same name as the function in the file.

The end keyword should be used to indicate the end of the function. It is required when any function in the file contains a nested function or function used as a local function within the script and function file. Functions work in scripts in R2016b or later. There are several types of functions used in MATLAB. They are as follows:

1. Anonymous Function

It is the function that is not stored in a program file, but it is associated with the variable whose data type is function_handle. It is defined in a single statement and has any number of input or output arguments. The syntax can be written as:

Fun=@(argumentlist)expression

Example:

mul=@(x,y) x*y;
res1=mul(2,3)
res2=mul(4,3)
res3=mul(4,5)

When we run the output, we get the following result:

res1=6

res2=12

res3=20

We can write anonymous functions with no inputs or multiple inputs and outputs. If the function has no input then we can use an empty parenthesis to call the anonymous function. Some of the examples are

1. curr= @() datestr(now);
d = curr()
Output: d= 22-Oct-2019 11:02:47

2. myfun=@(x,y)(x+y);
x=4
y=7
z=myfun(x,y)
Output: z=11

2. Local Functions

Any function file contains a primary function that appears in the first line of the code. They are visible to functions in other files and can be called in the command line. Other functions that are present in the file are called local functions. They cannot be called from the command line and are seen to a parent or main function and functions written in the same file. They are also known as sub-functions. They are equivalent to subroutines as used in another programming language. They can also be written in script files as long as they appear after the last line of the script code.

function [avg, med] = mystats(y)
a= length(y);
avg = mymean(y,a);
med = mymedian(y,a);
end

function a = mymean(v,n) ---- Example of a local function
a = sum(v)/n;
end

3. Nested Functions

Functions that are defined within another function or parent function are called nested functions. They can use or modify the variables that are defined in the parent function. They are defined within the scope of the function and can access the workspace in which they are defined. There are certain requirements that every nested function should follow:

  • All the functions do not require the end statement. However, to nest any function end statement should be written for every function.
  • We cannot define nested functions inside any control statements like if-else, switch case, etc.
  • Nested functions can be called directly by name or using any function handle.

function current
nestfun1
nestfun2
function nestfun1
x = 7;
end
function nestfun2
x = 4;
end

4. Private Functions

They are the functions that are visible only to a limited group of functions. They reside in sub-functions and are denoted using the keyword “private”. They are only visible to functions that are present in the parent folder or the functions in the folder immediately above the private subfolder. They are useful when we want to limit the scope of the function. We cannot call the private functions from the command line or from the functions outside of the parent folder.

Code:

function priv
disp(“Hello “)

Change the folder that contains a private folder and change the file name to present
function present
priv

Change the folder to any location and call the present function
present

Output:” Hello”

Conclusion – MATLAB Functions

 MATLAB Functions can be used for several tasks and scenarios. They form an integral part of any programming language. They can be accessed globally by using global variables or private functions if we want some information or content to be private. They are used in every organization for the business needs to fulfill the demands.

Recommended Articles

This is a guide to the MATLAB Functions. Here we discuss the Introduction of the MATLAB Functions and the Types of Functions. You can also go through our other suggested articles to learn more –

  1. Bessel Functions in MATLAB
  2. Anonymous Functions in Matlab
  3. Math Functions in C#
  4. Transfer Functions in Matlab
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests
 11+ Hours of HD Videos
5 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more