Updated March 21, 2023
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.
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 –