Support Articles

Support Articles Technical Computing Using Anonymous Functions for Passing Additional Parameters

Using Anonymous Functions for Passing Additional Parameters

A frequent MATLAB programming pattern we are seeing involves passing a function as an argument to some other function. For example, Optimization Toolbox functions require you to pass the objective function as an argument to the optimization solver; Handle Graphics objects and other objects in MATLAB (such as the timer object, or Data Acquisition, Image Acquisition, Instrument Control or OPC Toolbox objects) allow you to use anonymous functions in callbacks. Anonymous functions are also used to process events in MATLAB's new object-oriented programming features.

This technical article examines the use of anonymous functions to allow additional parameters to be passed to a calling function.

Contents

The Problem: Function functions with parameters

Very often, functions that require other functions as arguments (called "function functions") have a specific signature. A signature defines how you call a function. For example, quad approximates the integral of any function (the first parameter) between two limits (the second and third parameter). Here is an example of approximating the mathematical sin function between 0 and pi.

clear all
quad(@sin, 0, pi)

ans =
    2.0000

What happens, though, when the function you want to integrate includes additional parameters? For example, what if you want to calculate the integral of

for given a and b, over some range of t? The quad function signature does not support additional parameters for the integrating function.

The Solution: Anonymous functions to the rescue

Anonymous functions can help us to manage that process. First, define the values you want to use for a and b (this part might loop over a and b in a real application).

a = 5;
b = 0.2;


Now we define an anonymous function, which is a function declared as a variable rather than written as an m-file. Here's our anonymous function example, passing only the parameter to integrate over, and leaving the other parameters as the values passed.

myFun = @(t) a*sin(b*t)

myFun =
   @(t)a*sin(b*t)

Note how we can now call this function with only one parameter, even though it actually requires three.

myFun(2)

ans =
   1.9471

We can now pass this function on to the quad function to calculate the integral.

quad(myFun, 0, pi)

ans =
   4.7746

What about the other two parameters?

The other two parameters are not passed to the anonymous function. Instead, they are assigned from the value of a and b when the anonymous function is created. To show this, lets change the value of a and re-evaluate the anonymous function.

a = 0;
myFun(2)

ans =
   1.9471

Even though a now has a value of 0, the function still evaluates to 1.9471.

a*sin(b*2)

ans =
     0

Understanding anonymous function mechanics

When an anonymous function is created, the anonymous function expression is parsed, and all of the variables in the argument list are converted to dynamic variables (which are passed in the anonymous function call). In our example, only t becomes a variable in the anonymous function.

All of the additional variables to the right of the argument list (the variables in brackets to the left of the function expression) are evaluated, and their static values (the value at creation of the anonymous function) are assigned to that anonymous function.

Here is a rather strange error, caused by a variable udVar not being defined before creating an anonymous function.

myOtherFun = @(x) x + udVar
udVar = 2;
myOtherFun(2)

myOtherFun =
    @(x)x+udVar

Undefined function or variable 'udVar'.

Error in ==> evalmxdom>instrumentAndRun/@(x)x+udVar

Error in ==> anonymousFuncsExplained at 76
myOtherFun(2)

Using static assignment for passing additional parameters>

This static assignment of additional parameters can be very helpful in two situations:

  • Calling function functions that do not support additional arguments
  • Calling methods of a user-defined MATLAB object as a callback

Calling function functions

Most function functions require the function that is passed to have a specific syntax. For example, many Optimization Toolbox objective functions must accept only one variable (x), the independent variable. You can use anonymous functions to pass additional parameters to an objective function, when that parameter might not be initially known, or when you want to iterate over a wide range of values of that parameter.

For an example of this usage, search the Optimization Toolbox documentation for "Passing Extra Parameters", or type the following into a MATLAB command window:

docsearch('passing extra parameters')

Calling methods of a user-defined MATLAB object as a callback

Another common use of anonymous functions is to allow a callback to run a method on an object. By default, any callback is executed in the MATLAB base workspace. By setting a callback to an anonymous function, and declaring a method of an existing argument in the expression, you can call an object's method, even if that object is not in the base workspace. This works because the object is "in scope" when you create the anonymous function, and so is valid as long as that anonymous function is valid.

For more information on using anonymous functions for callbacks search the MATLAB help for "anonymous listener callback functions" or type the following into a MATLAB command window:

docsearch('anonymous listener callback functions')

Conclusion

Anonymous functions are a powerful way to pass additional arguments to function functions, or to execute methods of an object as callbacks, even if the object is not in the MATLAB base workspace. For more information on anonymous functions, see the MATLAB help:

docsearch('anonymous functions')