| Simulink Reference | ![]() |
Include MATLAB code in models that generate embeddable C code.
Library
Description
An Embedded MATLAB Function block lets you compose a MATLAB function in Simulink 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 Simulink and MATLAB, see Using the Embedded MATLAB Function Block in 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 Using the Embedded MATLAB 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 Simulink.
The Embedded MATLAB Function block supports a subset of the language for which it can generate efficient embeddable code. The following table gives a high-level overview of its capabilities with links to more detailed information.
See the Embedded MATLAB Function Block Reference for full details.
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 preceding limitations require you to code in a certain style. Some common idioms to avoid are listed in Limitations on Indexing Operations and Limitations with Complex Numbers in Simulink 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 Run-Time Function Library. These functions include functions in common categories like
plus, minus, and power
size, and length
lu, inv, svd, and chol
sin, cos, sinh, and cosh
to name just a few. See Embedded MATLAB Run-Time Library - by Category for a complete list of function categories.
Note
Although Embedded MATLAB attempts to produce exactly the same results as MATLAB, 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 Simulink 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 Argument and Return Variables, Sizing Function Argument and Return Variables, and Parameter Arguments in Embedded MATLAB Functions for more detailed descriptions of variables that you use in Embedded MATLAB Functions.
Data Type Support
The Embedded MATLAB Function block accepts inputs of any type supported by Simulink, except fixed-point types. For a discussion on the variable types supported by Embedded MATLAB functions in Simulink, refer to Data Types Supported by Simulink in the Simulink documentation.
Math with double and single types is supported. Integer math is not.
Simulink frames are not supported. However, you can use the Rate Transition block to convert frames into vectors.
Parameters and Dialog Box
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.
Characteristics
| Direct Feedthrough |
Yes |
| Sample Time |
Specified in the Sample time parameter |
| Scalar Expansion |
Yes |
| Dimensionalized |
Yes |
| Zero Crossing |
No |
| Dot Product | Enable | ![]() |
© 1994-2005 The MathWorks, Inc.