In some cases, you create functions that are extremely short. A common example is when you create a function to evaluate a mathematic expression, as demonstrated below:
function y = expr(x) y = x.*x + 2*x + 1; end
MATLAB provides anonymous functions to avoid the need of creating a function file in situations like this. An anonymous function:
- Has a single executable statement
- Does not require a function file
- Lives as a variable in the workspace until the variable is cleared
- Is visible as long as its variable exists
Create an anonymous function using the syntax
h = @(input1,…,inputN) expression, where:
his the function handle
input1,…,inputNis the set of input arguments
expressionis a single executable statement
If the function has no inputs, use empty parentheses. If
expression generates one or more outputs, request them when you call the function.
% Function that prints the text 'Hello World!' func1 = @() disp('Hello World!');
% Function that evaluates a polynomial func2 = @(x) x.*x + 2*x + 1;
% Function that calculates the perimeter of a rectangle func3 = @(a,b) 2 * (a + b);
% Function that finds the minimum value in a matrix func4 = @(A) min(A);
Anonymous functions are called as any other function, but using a function handle. You just need to pass a supported list of input arguments, and request a supported list of outputs.
% Call 'func1' func1();
% Call 'func2' y = func2(1);
y = 4
% Call 'func3' p = func3(2,3);
p = 10
Note that you can request as many output arguments as
expression supports or generates. However, in most cases anonymous functions have a single output.
% Call 'func4' to find the minimum value m1 = func4([3,5,1,7]);
m1 = 1
% Call 'func4' to find the minimum value and its index [m2,idx] = func4([3,5,1,7]);
m2 = 1
idx = 3
func4 is only provided to illustrate how to request multiple outputs. Do not create anonymous functions that simply wrap another function. Instead, call the target function directly.
Anonymous functions are called using a handle, which is a variable of type
function_handle. Given that variables have precedence over functions, anonymous functions have precedence over any other type of function.
Avoid creating handles that conflict with the name of any function, because this situation can generate unexpected results.
I recommend the following books to learn more on functions in MATLAB.
This book was the winner of a Textbook Excellence Award. The sixth chapter covers user-defined functions, scope of variables, debugging, and other aspects related to the development of MATLAB programs.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 6 explains how to create functions, including anonymous functions, local functions, and nested functions.
I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.
- How to Define Functions in MATLAB
- How to Call a Function in MATLAB
- How to Add Help Documentation to a Function in MATLAB
- Local Functions in MATLAB
- Nested Functions in MATLAB
- Visibility of Nested Functions in MATLAB
- Private Functions in MATLAB
- Anonymous Functions in MATLAB
- Inside the Handle of Anonymous Functions in MATLAB
- Functions with Variable Input Arguments in MATLAB
- Functions with Variable Output Arguments in MATLAB
- How to Validate the Number of Input Arguments Passed to a Function in MATLAB
- How to Validate the Number of Output Arguments Passed to a Function in MATLAB
I hope you have learned what anonymous functions are in MATLAB. The source code developed in this tutorial is available at this page.