Simulink Reference Previous page   Next Page
GEmbedded MATLAB Function

Include MATLAB code in models that generate embeddable C code.

Library

User-Defined Functions

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.

Supported MATLAB Features
Unsupported MATLAB Features
Two-Dimensional Arrays
N-Dimensional Arrays
Matrix operations(+,-,*,....)
Matrix Deletion (X(1) = [])
Logical Indexing
Complex Numbers
Sparse Matrices
Double/Single Math
Integer Math
if/switch/while/for
try-catch
Numeric Types
Cell Arrays, Structures, Java, User-Defined Classes
Subfunctions
Calling out to functions on the path (except for simulation)
persistent
global
Simulink Parameters as Inputs
Command Duality

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.

  1. 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.
  2. 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 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

to name just a few. See Embedded MATLAB Run-Time Library - by Category for a complete list of function categories.

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


Previous page  Dot Product Enable Next page

© 1994-2005 The MathWorks, Inc.