How to Declare Function Name, Inputs, and Outputs in MATLAB?
This blog will explore how to declare function names, inputs, and outputs in MATLAB.
How to Declare Function Name, Input, and Output Arguments in MATLAB?
We can declare function name, input, and output arguments in a line that is called a function definition line. This is the first executable line in a user-defined function. This line defines:
- The Function File
- The Function Name
- Number as well as Order of Input as well as Output Arguments
Syntax
The syntax for function definition in MATLAB is given below:
Here,
The statement function [y1,…,yN] = myfun(x1,…,xM) indicates the name of the function that is myfun, which receives the inputs x1,…,xM and returns the outputs y1,…,yN. Valid function names can contain letters, digits, or underscores, and an alphabetic character must be used as the first letter.
Note: It is a better approach to use the same file name as the function name but you can use different file names as well.
Examples
Consider some examples to understand how to declare function names, input, and output arguments in MATLAB.
Example 1: Taking Single Input and Returning Single Output
In the first example, we define a function with the function name vect_avg accepting a vector x as an input and returning a scalar value avg that is calculated as the average of the input vector x. We then save the function with the name vect_avg.m as a function file.
avg = sum(x(:))/length(x);
end
Now we call the function using the function file name in another script file to calculate the average of the given vector x.
avg = average(x)
Note: Make sure both m files should be placed in the same folder.
Execute the script file where you have called the function and it will display the calculated output avg of the input x:
You can also set the script file name differently from the declare function and then call this file in Command windows from the filename. This allows you to conveniently run the script without explicitly calling the function name within the script.
In the following example we change the function’s file name which is different from the function name say vect_mean.m and call the function on the command window using the specified function file name.
Example 2: Taking Single Input and Returning Multiple Outputs
The following example takes a single input and returns multiple outputs by declaring a function called avg_std_vect in MATLAB.
avg = sum(x)/length(x);
std = sqrt(sum((x-avg).^2/length(x)));
end
Example 3: Taking Multiple Inputs and Returning Single Output
The following example takes multiple inputs and returns single output by declaring a function called rect_area in MATLAB.
area = len*width;
end
Conclusion
You can declare function names, inputs, and outputs in MATLAB in a single line using the function definition line. This line serves as the initial declaration of the function, provides information about the function’s name, and includes a function name and variables it accepts as inputs and outputs. Using some examples, this guide has demonstrated the basic process of declaring a function name, input, and output arguments in MATLAB.
Source: linuxhint.com