Main Content

妈TLAB Function

Include妈TLABcode in models that generate embeddable C code

  • Library:
  • Simulink / User-Defined Functions

    HDL Coder / User-Defined Functions

  • 妈TLAB Function block

Description

With a妈TLAB Functionblock, you can write a MATLAB®function for use in a Simulink®model. The MATLAB function you create executes for simulation and generates code for aSimulink Coder™target. If you are new to the Simulink and MATLAB products, seeImplementing MATLAB Functions Using BlocksandCreate Custom Functionality Using MATLAB Function Blockfor an overview.

Double-clicking the MATLAB Function block opens its editor, where you write the MATLAB function, as in this example:

To learn more about this editor, see妈TLAB Function Block Editor.

You specify input and output data to the妈TLAB Functionblock in the function header as arguments and return values. The argument and return values of the preceding example function correspond to the inputs and outputs of the block in themodel:

You can also define data, input triggers, and function call outputs using the Ports and Data Manager, which you access from the MATLAB Function Block Editor by selectingEdit Data. SeePorts and Data Manager.

The妈TLAB Functionblock generates efficient embeddable code based on an analysis that determines the size, class, and complexity of each variable. This analysis imposes the following restrictions:

In addition to language restrictions, the妈TLAB Function块支持f的一金宝app个子集unctions available in MATLAB. A list of supported functions is given inFunctions and Objects Supported for C/C++ Code Generation. These functions include functions in common categories, such as:

Note

Although the code for this block attempts to produce exactly the same results as MATLAB, differences might occur due to rounding errors. These numerical differences, which might be a fewepsinitially, can magnify after repeated operations. Reliance on the behavior ofnanis not recommended. Different C compilers can yield different results for the same computation.

Note

In the妈TLAB Functionblock, the%#codegendirective is included to emphasize that the block’s MATLAB algorithm is always intended for code generation. The%#codegendirective, or the absence of it, does not change the error checking behavior in the context of the妈TLAB Functionblock. For more information seeCompilation Directive %#codegen.

To support visualization of data, the妈TLAB Functionblock supports calls to MATLAB functions for simulation only. SeeUse MATLAB Engine to Execute a Function Call in Generated Codeto understand some of the limitations of this capability, and how it integrates with code analysis for this block. If these function calls do not directly affect any of the Simulink inputs or outputs, the calls do not appear inSimulink Codergenerated code.

From妈TLAB Functionblocks, you can also call functions defined in a金宝app仿真软件的功能block. You can call Stateflow®functions withExport Chart Level Functions (Make Global)andAllow exported functions to be called by Simulinkchecked in the chart Properties dialog box.

In the Ports and Data Manager, you can declare a block input to be a Simulink parameter instead of a port. The妈TLAB Functionblock also supports inheritance of types and size for inputs, outputs, and parameters. You can also specify these properties explicitly. See函数参数类型,Size Function Arguments,Add Parameter Argumentsfor descriptions of variables that you use in妈TLAB Functionblocks.

Recursive calls are not allowed in妈TLAB Functionblocks.

By default,妈TLAB Functionblocks have direct feedthrough enabled. To disable it, in the Ports and Data Manager, clear theAllow direct feedthroughcheck box. Nondirect feedthrough semantics ensure that outputs rely only on the current state. Using nondirect feedthrough enables you to use妈TLAB Functionblocks in a feedback loop and prevent algebraic loops. For more information, seeUse Nondirect Feedthrough in a MATLAB Function Block.

Ports

Input

expand all

Input corresponding to the first input argument of the function inside the妈TLAB Functionblock. If you rename the function argument in the editor, the block renames the port correspondingly.

Data types supported by MATLAB but not supported by Simulink may not be passed between the Simulink model and the function within the妈TLAB Functionblock. These types may be used within the妈TLAB Functionblock.

For more information on fixed-point support for this block, refer toFixed-Point Data Types with MATLAB Function Block(Fixed-Point Designer)and妈TLAB Function Block with Data Type Override(Fixed-Point Designer).

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|Boolean|string|fixed point|enumerated|bus

nth input argument to the function in the妈TLAB Functionblock. When you add the argument in the editor, the block adds the port correspondingly.

Data types supported by MATLAB but not supported by Simulink may not be passed between the Simulink model and the function within the妈TLAB Functionblock. These types may be used within the妈TLAB Functionblock.

For more information on fixed-point support for this block, refer toFixed-Point Data Types with MATLAB Function Block(Fixed-Point Designer)and妈TLAB Function Block with Data Type Override(Fixed-Point Designer).

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|Boolean|string|fixed point|enumerated|bus

Output

expand all

First output argument of the function inside the妈TLAB Functionblock. If you rename the function argument in the editor, the block renames the port correspondingly.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|Boolean|string|fixed point|enumerated|bus

nth output argument from the function inside the妈TLAB Functionblock. When you add the argument in the editor, the block adds the port correspondingly.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|Boolean|string|fixed point|enumerated|bus

Block Characteristics

Data Types

Boolean[a]|bus[a]|double[a]|enumerated[a]|fixed point[a]|integer[a]|single[a]|string[a]

Direct Feedthrough

no

Multidimensional Signals

limited[a]

Variable-Size Signals

limited[a]

Zero-Crossing Detection

no

[a]Actual data type or capability support depends on block implementation.

Extended Capabilities

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Introduced in R2011a