Tutorial

Anonymous Functions in MATLAB

Anonymous functions do not require a function file and have a single executable statement.

Description

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

Definition

Create an anonymous function using the syntax h = @(input1,…,inputN) expression, where:

  • h is the function handle
  • input1,…,inputN is the set of input arguments
  • expression is 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);

Call

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);
% Call 'func3'
p = func3(2,3);

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]);
% Call 'func4' to find the minimum value and its index
[m2,idx] = func4([3,5,1,7]);

Note: 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.

Precedence

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.

Further reading

I recommend the following books to learn more on functions in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    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.

  2. 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.

web(fullfile(docroot, 'matlab/matlab_prog/anonymous-functions.html'));

Source code

I hope you have learned what anonymous functions are in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.