Anonymous Function

An anonymous function is a special function that does not need to live in a .m file, contains exactly one expression, and has class function handle.

  • Because it does not need to live in a .m file, an anonymous function can be created and called from the Command Window
  • Like an ordinary function, an anonymous function may have multiple input/output arguments; the rules are those of Matlab Functions Arguments

Creation

afun = @(arg1,...,argn) expression
  • afun is the function handle of the anonymous function — this is also the standard syntax for creating a function handle
  • arg1, …, argn are the formal input parameters
    • You cannot use a conditional statement to vary the body based on the actual argument count, so calls generally need to supply all formal arguments one-to-one
      • “Generally” again refers to the formal arguments that the expression actually uses
    • Of course, an anonymous function may take no inputs at all
  • expression is an expression in arg1, …, argn

Remarks

  • The expression of an anonymous function may call other functions, and anonymous functions may even be nested
  • Unlike a function handle for an ordinary function, an anonymous function’s handle stores the entire definition of the function it represents
  • An anonymous function has its own function workspace; uniquely, it can read variables from the base workspace at creation time, but those values are then frozen as constants in its workspace and disconnected from the base workspace
    • Example:
>> a = 1;
>> f = @(x) x .* a;
>> disp(f(1))
    1
>> a = 5;
>> disp(f(1))
    1
>> clear a
>> disp(f(1))
    1