Matlab

# The Inline Function in MATLAB

This article will explain using the inline() function to construct function objects in MATLAB. We will look at the different methods this function provides, and we will show you how to use each with pictures and practical examples. We will also go into more detail about the input and output arguments and the type of data each of these arguments accepts.

IMPORTANT: Mathworks will remove this function from the MATLAB library in future releases. Therefore, we recommend using anonymous functions instead of those created with inline().

## MATLAB inline() Function Syntax

f = inline (ex)

f = inline (ex, N)

f = inline (ex, arg1, arg2… argn)

## Description and Examples of MATLAB inline() Function

MATLAB’s inline() function generates functions inline from code. Inline returns a function in “Func” that, when called, has the same behavior and attributes as a standard function and returns its output as the result of the expression or function specified when the object was created. A function created with this method can be used anywhere in the code without requiring further declaration. MATLAB’s inline() function provides two ways to create these functions. One is to create them from user-defined or user-created mathematical expressions or formulas. Another way is to use expressions that contain functions from the MATLAB library. To create a function using this method, we need to declare the explicit expression in the form of character strings in the input arguments of inline(). Now, let us see how we create a function, “Func”, from the expression a + b:

Func = inline('a + b')

This way, we created the function “Func”, which, when called, returns the result of the addition of a + b. This is the easiest way to create an inline() function from a user-created expression.

Func = inline ('function (arg1, arg2)')

Or from a function whose input arguments are explicitly declared:

Func = inline ('function (arg1, arg2)', 'arg2', 'arg1')

As you can see in the previous case, both the expression and the input arguments in inline() are declared as comma-separated character vectors. The following figure shows the object created for the previous expression: When an inline function is created using a function, the input arguments are determined by recognizing an alphanumeric lowercase character that is not part of a word.

The input arguments for the inline() function are the following:

exp: This is the input expression. The data type for “exp” is a character string.

arg1, arg2, arg…n: These are the input arguments of the created function. The data type for these entries is character strings.

N: This positive scalar indicates the number of input arguments.

Next, look at some practical examples and application images of the MATLAB inline() function.

## Example 1: To Create and Use an Inline Function From an Expression to Calculate the RMS Value of a Sine Wave

In this example, we will create a simple console application to calculate the RMS value of a sine wave from a function created with inline(). To do this, we make the function “RMS”. Since the wave is sinusoidal, we calculate its RMS value using the following formula:

RMS = Vpk1.4142

This formula is converted to a function by inline(). Each time it is called from any part of the script in “r”, it returns the RMS value of the value entered in “x”.

To create the function RMS, we call inline(), sending it as an input argument character string containing the explicit expression of the formula for finding the RMS value of the peak-to-peak voltage (Vpp) “x”. Once the RMS function is created for our calculation, we use the prompt function to enter the Vpp from which we want to obtain the RMS value in the command console.

RMS= inline ('x./1.4142');

while 1

clc();

prompt = 'Enter Vpp';

x =input(prompt);

r=RMS(x);

disp( [ 'RMS is: ' , num2str(r)])

%disp (r);

prompt = 'Press Enter to continue or CTRL+c to exit';

x =input(prompt);

end

Now that we have created our function RMS (x), we can use it to calculate the RMS values of sine waves based on their peak voltage. Once created, this function can be called from anywhere in the code. ## Example 2: To Create and Use an Online Function From a Function

Now, let us look at how you can use inline() to create a function from a function in the MATLAB library. In this example, we create a console application in which we create the function “r” that returns the modulus of dividing the dividend “a” by the divisor “b”. To do this, we use the mod() function from the MATLAB library:

r = inline ('mod(a, b)', ‘a’, ‘b’);

while 1

clc();

prompt = 'Enter dividendo';

a =input(prompt);

prompt = 'Enter divisor';

b =input(prompt);

d= a./b;

m= r(a, b);

disp( [ 'The result is: ' , int2str(d),'. The module is: ' , num2str(m)] )

prompt = 'Press Enter to continue or CTRL+c to exit';

x =input(prompt);

end ## More Reliable Alternative to the inline() Function of MATLAB

As we mentioned, the inline() function is abolished in MATLAB. So, we recommend creating the functions we will use in our code as anonymous functions with the special character “@”. Next, we will briefly show how you can create functions using this method.

## How To Create a Function in MATLAB Using an Alternative Method to inline()

Now, let us look at an example of the simplest way to create an anonymous function using the special character “@”. Next, we will look at the syntax for declaring a function using this mode:

name [email protected] (input arguments) function

The name of the function must precede the “@” character. Then, the input arguments must be declared in parentheses and separated by commas if there is more than one and then the expression. Next, we repeat Example 1 but replace inline() with this method to create the RMS function:

RMS = @ (x) x./1.4142;

while 1

clc();

prompt = 'Enter Vpp';

x =input(prompt);

r=RMS(x);

disp( [ 'RMS is: ' , num2str(r)])

%disp (r);

prompt = 'Press Enter to continue or CTRL+c to exit';

x =input(prompt);

end ## Conclusion

This article explained how to create functions using MATLAB inline() function. The inline() function is one of several tools this powerful programming language offers for creating functions. We included practical examples and pictures showing how you can create functions from mathematical formulas entered by the programmer and functions in the MATLAB library. We also showed you more reliable alternatives for creating functions, as Mathworks has announced that the inline() function will no longer be used in future versions. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

### About the author #### Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).