Next: , Previous: , Up: Function Handles Anonymous Functions Inline Functions   [Contents][Index]

11.11.2 Anonymous Functions

Anonymous functions are defined using the syntax

@(argument-list) expression

Any variables that are not found in the argument list are inherited from the enclosing scope. Anonymous functions are useful for creating simple unnamed functions from expressions or for wrapping calls to other functions to adapt them for use by functions like quad. For example,

f = @(x) x.^2;
quad (f, 0, 10)
    ⇒ 333.33

creates a simple unnamed function from the expression x.^2 and passes it to quad,

quad (@(x) sin (x), 0, pi)
    ⇒ 2

wraps another function, and

a = 1;
b = 2;
quad (@(x) betainc (x, a, b), 0, 0.4)
    ⇒ 0.13867

adapts a function with several parameters to the form required by quad. In this example, the values of a and b that are passed to betainc are inherited from the current environment.