| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
The Embedded MATLAB Function block allows you to add MATLAB functions to Simulink models for deployment to embedded processors. This capability is useful for coding algorithms that are better stated in the textual language of the MATLAB software than in the graphical language of the Simulink product. This block works with a subset of the MATLAB language called the Embedded MATLAB subset, which provides optimizations for generating efficient, production-quality C code for embedded applications. For more information, see Working with the Embedded MATLAB Subset in the MATLAB documentation. For more information on fixed-point support in MATLAB, refer to Working with the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox documentation.
Here is an example of a Simulink model that contains an Embedded MATLAB Function block:

You will build this model in Creating an Example Embedded MATLAB Function.
Defining Local Variables. Note in this Embedded MATLAB function that you can declare local variables implicitly through assignment, just as you would in MATLAB functions. The variable takes its type and size from the context in which it is assigned. For example, the following code line declares x to be a scalar variable of type double.
x = 1.54;
Once you define a variable, you cannot redefine it to any other type or size in the function body. For example, you cannot declare x and reassign it:
x = 2.65; % OK: x is a scalar double x = [x 2*x]; % Error: x cannot be changed to a vector
See Creating Local Complex Variables By Assignment in the Embedded MATLAB documentation for detailed descriptions and examples.
Declaring Extrinsic Functions. Note in this example, that you can declare functions to be extrinsic by using eml.extrinsic. This ensures that the Embedded MATLAB software does not attempt to compile this function. Instead, the function will execute in the MATLAB workspace during simulation of the model. For example, the following code declares the plot function to be extrinsic:
eml.extrinsic('plot');See Calling Embedded MATLAB Library Functions in the Embedded MATLAB documentation for more information.
In addition to supporting a rich subset of the MATLAB language, Embedded MATLAB Function blocks can call any of the following types of functions:
Subfunctions are defined in the body of the Embedded MATLAB block. In the preceding example, avg is a subfunction. See Calling Subfunctions in the Embedded MATLAB documentation.
Embedded MATLAB runtime library functions
Embedded MATLAB runtime library functions are a subset of the functions that you call in MATLAB. When you build your model with Real-Time Workshop, these functions generate C code that conforms to the memory and variable type requirements of embedded environments. In the preceding example, length, sqrt, and sum are Embedded MATLAB runtime library functions. See Calling Embedded MATLAB Library Functions in the Embedded MATLAB documentation.
Function calls that cannot be resolved as subfunctions or Embedded MATLAB runtime library functions are extrinsic functions and are resolved in the MATLAB workspace. These functions do not generate code; they execute only in the MATLAB workspace during simulation of the model. The Embedded MATLAB subset attempts to compile all MATLAB functions unless you explicitly declare them to be extrinsic by using eml.extrinsic. See Calling MATLAB Functions in the Embedded MATLAB documentation.
Embedded MATLAB Function blocks provide the following capabilities:
Allow you to build MATLAB functions into embeddable applications — Embedded MATLAB Function blocks support a subset of MATLAB commands that generate efficient C code (see Embedded MATLAB Function Library Reference in the Embedded MATLAB documentation). With this support, you can use Real-Time Workshop to generate embeddable C code from Embedded MATLAB Function blocks that implements a variety of sophisticated mathematical applications. In this way, you can build executables that harness MATLAB functionality, but run outside the MATLAB environment.
Inherit properties from Simulink input and output signals — By default, both the size and type of input and output signals to an Embedded MATLAB Function block are inherited from Simulink signals. You can also choose to specify the size and type of inputs and outputs explicitly in the Model Explorer or Ports and Data Manager (see Ports and Data Manager).
![]() | Using the Embedded MATLAB Function Block | Creating an Example Embedded MATLAB Function | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |