Functions
Both scripts and functions allow you to reuse sequences of commands by storing them in program files. Scripts are the simplest type of program, since they store commands exactly as you would type them at the command line. Functions provide more flexibility, primarily because you can pass input values and return output values.
Function with one output
For example, this function named fun
computes the following function of two variables and returns the result f
.
f = 20(x2 − x13)3 − (7 − x2)2
Open a new MATLAB script fun.m
and type the following;
This type of function must be defined within a file, not at the command line. Often, you store a function in its own file. In that case, the best practice is to use the same name for the function and the file (in this example, fun.m), since MATLAB® associates the program with the file name. Save the file either in the current folder or in a folder on the MATLAB search path.
You can call the function from the command line, using the same syntax rules that apply to functions installed with MATLAB. For instances, calculate f(3,7)
Function with multiple outputs
Define a function in a file named stat.m that returns the mean and variance of an input vector.
Call the function from the command line.
Multiple functions in a function file
Define two functions in a file named stat2.m, where the first function calls the second.
function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end
Function avg is a local function. Local functions are only available to other functions within the same file.
Call function stat2 from the command line.
Function with argument validation
Define a function that restricts input to a numeric vector that contains no Inf or NaN elements. This function uses the arguments keyword, which is valid for MATLAB® versions R2019b and later.
function [m,s] = stat3(x)
arguments
x (1,:) {mustBeNumeric, mustBeFinite}
end
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end
In the arguments code block, ( 1, : ) indicates that x must be a vector. The validation functions, {mustBeNumeric, mustBeFinite}, restrict the elements in x to numeric values that are not Inf or NaN. For more information, see Function Argument Validation.
Calling the function with a vector that contains an element that is NaN violates the input argument declaration. This violation results in an error being thrown by the mustBeFinite validation function.
Invalid input argument at position 1. Value must be finite.
Function in a script file
Define a script in a file named integrationScript.m that computes the value of the integrand at and computes the area under the curve from and Include a local function that defines the integrand,
x = pi/2;
y = myIntegrand(x)
% Compute the value of the integrand at pi/2.
xmin = 0;
xmax = pi/2;
% Compute the area under the curve from 0 to pi/2.
f = @myIntegrand;
a = integral(f,xmin,xmax)
function y = myIntegrand(x)
y = cos(x);
end
Note: Including functions in scripts requires MATLAB® R2016b or later.