MATLAB For Dummies
Book image
Explore Book Buy On Amazon

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.

About This Article

This article is from the book:

About the book authors:

John Paul Mueller is an author and technical editor with experience in application development, database management, machine learning, and deep learning. He has written hundreds of books and articles helping everyday people learn everything from networking to database management.

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: