| Simulink® | ![]() |
User-Defined Functions

An Embedded MATLAB™ Function block lets you compose a MATLAB function within a Simulink model like the following example:

The MATLAB function you create executes for simulation and generates code for a Real-Time Workshop target. If you are new to the Simulink and MATLAB products, see Using the Embedded MATLAB Function Block in the Simulink documentation for a comprehensive overview including a step-by-step example.
You create the MATLAB function in the Embedded MATLAB Editor. To learn about this editor's capabilities see Embedded MATLAB Function Editor.
You specify input and output data to the Embedded MATLAB Function block in the function header as arguments and return values. Notice that the argument and return values of the preceding example function correspond to the inputs and outputs of the block in the Simulink model.

The Embedded MATLAB Function block supports a subset of the language for which it can generate efficient embeddable code. For details about the Embedded MATLAB subset, see Working with the Embedded MATLAB Subset in the Embedded MATLAB documentation.
To generate embeddable code, the Embedded MATLAB Function block relies on an analysis that determines the size and class of each variable. This analysis imposes the following additional restrictions on the way in which the above features may be used.
The first definition of a variable must define both its class and size. The class and size of a variable cannot be changed once it has been set.
Whether data is complex or real is determined by the first definition. Subsequent definitions may assign real numbers into complex storage but may not assign complex numbers into real storage.
The preceding limitations require you to code in a certain style. Some common idioms to avoid are listed in "Using Matrix Indexing Operations" and "Working with Complex Numbers" in the Embedded MATLAB documentation.
In addition to language restrictions, Embedded MATLAB Function blocks support only a subset of the functions available in MATLAB. A list of supported functions is given in the Embedded MATLAB Function Library Reference in the Embedded MATLAB documentation. These functions include functions in common categories like
Arithmetic functions like plus, minus, and power
to name just a few. See Embedded MATLAB Function Library — Categorical List in the Embedded MATLAB documentation for a complete list of function categories.
Note Although Embedded MATLAB software attempts to produce exactly the same results as MATLAB software, there will be occasions when they will differ due to rounding errors. These numerical differences, which may be a few eps initially, might be magnified after repeated operations. Reliance on the behavior of nan is not recommended. Different C compilers may yield different results for the same computation. |
To support visualization of data, Embedded MATLAB Function blocks support calls to MATLAB functions for simulation only. See Calling MATLAB Functions in the Embedded MATLAB documentation to understand some of the limitations of this capability, and how it is integrated into Embedded MATLAB analysis. If these calls do not directly affect any of the Simulink inputs or outputs, they are eliminated from the generated code when generating code with Real-Time Workshop.
You can declare an Embedded MATLAB input to be a Simulink parameter instead of a port in the Model Explorer. The Embedded MATLAB Function block also supports inheritance of types and size for inputs, outputs, and parameters. If needed, you can also set these explicitly using the Model Explorer. See Typing Function Arguments, Sizing Function Arguments, and Parameter Arguments in Embedded MATLAB Functions, for more detailed descriptions of variables that you use in Embedded MATLAB Functions.
Note that recursive calls are not allowed in Embedded MATLAB functions.
The Embedded MATLAB Function block accepts inputs of any type supported by Simulink software, except enumerated types. For a discussion on the variable types supported by Embedded MATLAB functions in Simulink software, refer to Data Types Supported by Simulink in the Simulink documentation.
For more information on fixed-point support in Embedded MATLAB, refer to Working with the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox™ documentation.
The Embedded MATLAB Function block supports Simulink frames. See Frame-Based Signals in the Signal Processing Blockset documentation for more information.
The Block Parameters dialog box for an Embedded MATLAB Function block is identical to the Block Parameters dialog box for a Subsystem block. See the reference page for the Subsystem, Atomic Subsystem, CodeReuse Subsystem blocks for an identification of each field.
Direct Feedthrough | Yes |
Sample Time | Specified in the Sample time parameter |
Scalar Expansion | Yes |
Dimensionalized | Yes |
Multidimensionalized | Yes |
Zero Crossing | No |
![]() | Dot Product | Enable | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |