MATLAB Function
IncludeMATLABcode in models that generate embeddable C code
Description
With aMATLAB 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, seeMATLAB Function Block Editor.
You specify input and output data to theMATLAB 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.
TheMATLAB 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, theMATLAB 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:
Arithmetic operators likeplus
,minus
, andpower
. For more information, seeArray vs. Matrix Operations.
Matrix operations likesize
, andlength
Advanced matrix operations likelu
,inv
,svd
, andchol
Trigonometric functions likesin
,cos
,sinh
, andcosh
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 feweps
initially, can magnify after repeated operations. Reliance on the behavior ofnan
is not recommended. Different C compilers can yield different results for the same computation.
Note
In theMATLAB Functionblock, the%#codegen
directive is included to emphasize that the block’s MATLAB algorithm is always intended for code generation. The%#codegen
directive, or the absence of it, does not change the error checking behavior in the context of theMATLAB Functionblock. For more information seeCompilation Directive %#codegen.
To support visualization of data, theMATLAB Functionblock supports calls to MATLAB functions for simulation only. SeeExtrinsic Functionsto 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.
FromMATLAB 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. TheMATLAB Functionblock also supports inheritance of types and size for inputs, outputs, and parameters. You can also specify these properties explicitly. SeeType Function Arguments,Size Function Arguments, andAdd Parameter Argumentsfor descriptions of variables that you use inMATLAB Functionblocks.
Recursive calls are not allowed inMATLAB Functionblocks.
By default,MATLAB 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 useMATLAB Functionblocks in a feedback loop and prevent algebraic loops. For more information, seeUse Nondirect Feedthrough in a MATLAB Function Block.
Ports
Input
expand all
u
— Input argumentu
scalar | vector | matrix
Input corresponding to the first input argument of the function inside theMATLAB 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 theMATLAB Functionblock. These types may be used within theMATLAB Functionblock.
For more information on fixed-point support for this block, refer toFixed-Point Data Types with MATLAB Function Block(Fixed-Point Designer)andMATLAB 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
n
— Input argumentn
scalar | vector | matrix
n
th input argument to the function in theMATLAB 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 theMATLAB Functionblock. These types may be used within theMATLAB Functionblock.
For more information on fixed-point support for this block, refer toFixed-Point Data Types with MATLAB Function Block(Fixed-Point Designer)andMATLAB 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
y
— Output argument
scalar | vector | matrix
First output argument of the function inside theMATLAB 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
n
— Output argumentn
scalar | vector | matrix
n
th output argument from the function inside theMATLAB 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 |double |enumerated |fixed point |integer |single |string
|
Direct Feedthrough |
no
|
Multidimensional Signals |
limited
|
Variable-Size Signals |
limited
|
Zero-Crossing Detection |
no
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Actual data type or capability support depends on block implementation.
HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic. Actual data type or capability support depends on block implementation. For best practices of using theMATLAB Functionblock, see:
HDL Architecture
MATLAB Function (Default) |
This is the default architecture forMATLAB Functionblocks with fixed-point data types. When you use this architecture, you can use speed and area optimizations for blocks that surround theMATLAB Functionblock. The code inside theMATLAB Functionblock is not optimized. |
MATLAB Datapath |
This is the default architecture forMATLAB Functionblocks with floating-point data types. When you use this architecture, the code generator treats theMATLAB Functionblock like a regularSubsystemblock. By enabling this architecture for fixed-point types, you can use speed and area optimizations for blocks inside theMATLAB Functionblock and across theMATLAB Functionblock with other Simulink blocks. This capability enables you to specify additional properties for theMATLAB Functionblock that you would specify for aSubsystemsuch asClockRatePipeliningandFlattenHierarchy. |
HDL Block Properties
AdaptivePipelining |
Automatic pipeline insertion based on the synthesis tool, target frequency, and multiplier word-lengths. The default isinherit . To specify this property, setArchitecturetoMATLAB Datapath . See alsoAdaptivePipelining(HDL Coder). |
BalanceDelays |
Detects introduction of new delays along one path and inserts matching delays on the other paths. The default isinherit . To specify this property, setArchitecturetoMATLAB Datapath . See alsoBalanceDelays(HDL Coder). |
ClockRatePipelining |
Insert pipeline registers at a faster clock rate instead of the slower data rate. The default isinherit . To specify this property, setArchitecturetoMATLAB Datapath . See alsoClockRatePipelining(HDL Coder). |
ConstMultiplierOptimization |
Canonical signed digit (CSD) or factored CSD optimization. The default isnone . See alsoConstMultiplierOptimization(HDL Coder). |
ConstrainedOutputPipeline |
寄存器的数量在mov的输出ing existing delays within your design. Distributed pipelining does not redistribute these registers. The default is0 . For more details, seeConstrainedOutputPipeline(HDL Coder). |
DistributedPipelining |
Pipeline register distribution, or register retiming. The default isoff . See alsoDistributedPipelining(HDL Coder). |
DSPStyle |
Synthesis attributes for multiplier mapping. The default isnone . To specify this property, setArchitecturetoMATLAB Datapath . See alsoDSPStyle(HDL Coder). |
FlattenHierarchy |
Remove subsystem hierarchy from generated HDL code. The default isinherit . To specify this property, setArchitecturetoMATLAB Datapath . See alsoFlattenHierarchy(HDL Coder). |
InputPipeline |
Number of input pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0 . For more details, seeInputPipeline(HDL Coder). |
InstantiateFunctions |
Generate a VHDL®entity or Verilog®module for each function. The default isoff . See alsoInstantiateFunctions(HDL Coder). |
LoopOptimization |
Unroll, stream, or do not optimize loops. The default isnone . If you setArchitecturetoMATLAB Datapath , you can only unroll loops. If you want to use loop streaming with theMATLAB Datapath architecture, you can use the streaming optimization by specifying aStreamingFactor. See alsoLoopOptimization(HDL Coder). |
MapPersistentVarsToRAM |
Map persistent arrays to RAM. The default isoff . See alsoMapPersistentVarsToRAM(HDL Coder). |
OutputPipeline |
Number of output pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0 . For more details, seeOutputPipeline(HDL Coder). |
ResetType |
抑制复位逻辑代. The default isdefault , which generates reset logic. See alsoResetType(HDL Coder). |
SharingFactor |
Number of functionally equivalent resources to map to a single shared resource. The default is 0. See alsoResource Sharing(HDL Coder). |
StreamingFactor |
Number of parallel data paths, or vectors, that are time multiplexed to transform into serial, scalar data paths. The default is 0, which implements fully parallel data paths. To specify this property, setArchitecturetoMATLAB Datapath . See alsoStreaming(HDL Coder). |
VariablesToPipeline |
Insert a pipeline register at the output of the specified variable or variables. Specify the list of variables as a character vector, with spaces separating the variables. To specify this property, setArchitecturetoMATLAB Function . |
Restrictions
If the block contains a System object™, block inputs cannot have non-discrete (constant orInf
) sample time.
HDL Coder does not support aMATLAB Functionthat contains the same variable as the input and output of the function. For example, this MATLAB code is not supported.
For the MATLAB language subset supported for HDL code generation from aMATLAB Functionblock, seeSupported MATLAB Data Types, Operators, and Control Flow Statements(HDL Coder).
PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.
Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.
Actual data type or capability support depends on block implementation.
Introduced in R2011a