Introduction to MATLAB Functions
MATLAB Functions are written with various lines of code which 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 which 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:
- 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 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 is 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:
When we run the output we get the following result:
4.8 (3,610 ratings)
We can write anonymous functions with no inputs or multiple inputs and outputs. If the function has no input then we can use empty parenthesis to call the anonymous function. Some of the examples are
curr= @() datestr(now);
d = curr()
Output: d= 22-Oct-2019 11:02:47
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)
avg = mymean(y,a);
med = mymedian(y,a);
function a = mymean(v,n) ---- Example of a local function
a = sum(v)/n;
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 which 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.
x = 7;
x = 4;
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 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.
Change the folder that contains a private folder and change the file name to present
Change the folder to any location and call the present function
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 both globally by using global variables or using 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.
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 –