How to Use MATLAB’s Inline and Anonymous Functions

By Jim Sizemore, John Paul Mueller

MATLAB supports a few interesting additions to the standard functions. In general, these additions are used to support complex applications that require unusual programming techniques. However, it pays to know that the functions exist for situations in which they come in handy.

Inline functions

An inline function is one that performs a small task and doesn’t actually reside in a function file. You can create an inline function right in the Command window if you want. The main purpose for an inline function is to make it easier to perform a calculation or manipulate data in other ways.

You use an inline function as a kind of macro. Instead of typing a lot of information every time, you define the inline function once and then use the inline function to perform all the extra typing.

To see an inline function in action, type SayHello8 = inline(‘[‘‘Hello There ”, Name, ‘‘!”]’) in the Command window and press Enter. You see the following output:

SayHello8 =
  Inline function:
  SayHello8(Name) = [‘Hello There ‘, Name, ‘!’]

This function returns a combined greeting string. All you need to do is type the function name and supply the required input value. Test this inline function by typing disp(SayHello8(‘Robert’)) and pressing Enter. You see the expected output:

Hello There Robert!

Notice that the inline function doesn’t actually include the disp() function call. An inline function must return a value, not perform output. If you try to include the disp() function call, you see the following error message:

Error using inlineeval (line 15)
Error in inline expression ==> disp([‘Hello There ‘, Name, ‘!’])
 Too many output arguments.
Error in inline/subsref (line 24)
     INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Anonymous functions

An anonymous function is an even shorter version of the inline function. It can contain only a single executable statement. The single statement can accept input arguments and provide output data.

To see how an anonymous function works, type SayHello9 = @(Name) [‘Hello There ‘, Name, ‘!’] and press Enter. You see the following output:

SayHello9 =
 @(Name)[‘Hello There ‘,Name,’!’]

The at (@) symbol identifies the code that follows as an anonymous function. Any input arguments you want to accept must appear in the parentheses that follow the @ symbol. The code follows after the input argument declaration. In this case, you get yet another greeting as output.

To test this example, type disp(SayHello9(‘Evan’)) in the Command window and press Enter. You see the following output:

Hello There Evan!

You generally use anonymous functions for incredibly short pieces of code that you need to use repetitively. Inline functions execute more slowly than anonymous functions for a comparable piece of code. So whenever possible, use an anonymous function in place of an inline function.

However, inline functions also provide the extra flexibility of allowing multiple lines of code, so you need to base your decision partly on how small you can make the code that you need to execute.