MATLAB Function
Include MATLAB code in
models that generate embeddable C code
Description
With a MATLAB Function block, you can write a MATLAB^{®} function
for use in a Simulink^{®} model. The MATLAB function you create
executes for simulation and generates code for a Simulink
Coder™ target.
If you are new to the Simulink and MATLAB products, see Implementing MATLAB Functions Using Blocks and Create Custom Functionality Using MATLAB Function Block for an overview.
Doubleclicking the MATLAB Function block opens its editor,
where you write the MATLAB function, as in this example:
To learn more about this editor, see MATLAB Function Block Editor.
You specify input and output data to the MATLAB Function block
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 the model:
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 selecting Edit Data.
See Ports and Data Manager.
The MATLAB Function block 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 MATLAB Function block supports a
subset of the functions available in MATLAB. A list of supported functions is given in Functions and Objects Supported for C/C++ Code Generation. These functions
include functions in common categories, such as:
Arithmetic operators like plus
, minus
,
and power
. For more information, see Array vs. Matrix Operations (MATLAB).
Matrix operations like size
,
and length
Advanced matrix operations like lu
, inv
, svd
, and chol
Trigonometric functions like sin
, cos
, sinh
,
and cosh
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 few eps
initially, can magnify after repeated
operations. Reliance on the behavior of nan
is not recommended. Different C compilers can yield different results for the same computation.
Note
In the MATLAB Function block, 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 the MATLAB Function block. For more
information see Compilation Directive %#codegen.
To support visualization of data, the MATLAB Function block
supports calls to MATLAB functions for simulation only. See Extrinsic Functions to
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 in Simulink
Coder generated code.
From MATLAB Function blocks, you can also call
functions defined in a Simulink Function block. You
can call Stateflow^{®} functions with Export Chart Level Functions (Make Global) and Allow exported functions to be called by Simulink checked 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 MATLAB Function block also supports inheritance of types and size
for inputs, outputs, and parameters. You can also specify these properties
explicitly. See Type Function Arguments, Size Function Arguments,
and Add Parameter Arguments for
descriptions of variables that you use in MATLAB Function blocks.
Recursive calls are not allowed in MATLAB Function blocks.
By default, MATLAB Function blocks have direct feedthrough enabled. To disable
it, in the Ports and Data Manager, clear the Allow direct feedthrough check box. Nondirect feedthrough enables semantics to ensure
that outputs rely only on current state. Using nondirect feedthrough enables you to use
MATLAB Function blocks in a feedback loop and prevent algebraic loops.
Ports
Input
expand all
u
— Input argument u
scalar  vector  matrix
Input corresponding to the first input argument of the function
inside the MATLAB Function block. 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 MATLAB
Function block. These types may be used within the
MATLAB Function block.
For more information on fixedpoint support for this block, refer to
FixedPoint Data Types with MATLAB Function Block (FixedPoint Designer)
and MATLAB Function Block with Data Type Override (FixedPoint Designer).
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
 enumerated
 bus
n
— Input argument n
scalar  vector  matrix
n
th input argument to the function in the
MATLAB Function block. 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 MATLAB
Function block. These types may be used within the
MATLAB Function block.
For more information on fixedpoint support for this block, refer to
FixedPoint Data Types with MATLAB Function Block (FixedPoint Designer)
and MATLAB Function Block with Data Type Override (FixedPoint Designer).
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 Boolean
 fixed point
 enumerated
 bus
Output
expand all
y
— Output argument
scalar  vector  matrix
First output argument of the function inside the MATLAB
Function block. 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
 fixed point
 enumerated
 bus
n
— Output argument n
scalar  vector  matrix
n
th output argument from the function inside the
MATLAB Function block. 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
 fixed point
 enumerated
 bus
Block Characteristics
Data Types  Boolean^{[a]}  bus  double  enumerated  fixed point  integer  single

Direct Feedthrough  no

Multidimensional Signals  limited

VariableSize Signals  limited

ZeroCrossing 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 the MATLAB
Function block, see:
HDL Architecture
MATLAB Function (Default)  This is the default architecture for MATLAB
Function blocks with fixedpoint data types.
When you use this architecture, you can use speed and area
optimizations for blocks that surround the MATLAB
Function block. The code inside the
MATLAB Function block is not
optimized. 
MATLAB Datapath  This is the default architecture for MATLAB Function blocks with
floatingpoint data types. When you use this architecture,
the code generator treats the MATLAB Function
block like a regular Subsystem block. By
enabling this architecture for fixedpoint types, you can
use speed and area optimizations for blocks inside the
MATLAB Function block and across the
MATLAB Function block with other
Simulink blocks. This capability enables
you to specify additional properties for the MATLAB
Function block that you would specify for a
Subsystem such as
ClockRatePipelining and
FlattenHierarchy. 
HDL Block Properties
AdaptivePipelining  Automatic pipeline insertion based on the synthesis tool, target frequency, and
multiplier wordlengths. The default is
inherit . To specify this
property, set Architecture to
MATLAB Datapath . See also AdaptivePipelining (HDL Coder). 
BalanceDelays  Detects introduction of new delays along one path and
inserts matching delays on the other paths. The default is
inherit . To specify this
property, set Architecture to
MATLAB Datapath . See also BalanceDelays (HDL Coder). 
ClockRatePipelining  Insert pipeline registers at a faster clock rate
instead of the slower data rate. The default is
inherit . To specify this
property, set Architecture to
MATLAB Datapath . See also ClockRatePipelining (HDL Coder). 
ConstMultiplierOptimization  Canonical signed digit (CSD) or factored CSD optimization. The
default is none . See also ConstMultiplierOptimization (HDL Coder).

ConstrainedOutputPipeline  Number of registers to place at
the outputs by moving existing delays within your design. Distributed
pipelining does not redistribute these registers. The default is
0 . For more details, see ConstrainedOutputPipeline (HDL Coder).

DistributedPipelining  Pipeline register distribution, or register retiming. The default
is off . See also DistributedPipelining (HDL Coder).

DSPStyle  Synthesis attributes for multiplier mapping. The default is
none . To specify this
property, set Architecture to
MATLAB Datapath . See also DSPStyle (HDL Coder). 
FlattenHierarchy  Remove subsystem hierarchy from generated HDL code. The
default is inherit . To specify
this property, set Architecture to
MATLAB Datapath . See also FlattenHierarchy (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 is
0 . For more details, see InputPipeline (HDL Coder).

InstantiateFunctions  Generate a VHDL^{®} entity or Verilog^{®} module for
each function. The default is off . See
also InstantiateFunctions (HDL Coder).

LoopOptimization  Unroll, stream, or do not optimize loops. The default is
none . If you set
Architecture to MATLAB
Datapath , you can only unroll loops. If
you want to use loop streaming with the MATLAB
Datapath architecture, you can use the
streaming optimization by specifying a
StreamingFactor. See also LoopOptimization (HDL Coder). 
MapPersistentVarsToRAM  Map persistent arrays to RAM. The default is off .
See also MapPersistentVarsToRAM (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 is
0 . For more details, see OutputPipeline (HDL Coder).

ResetType  Suppress reset logic generation. The default is default ,
which generates reset logic. See also ResetType (HDL Coder).

SharingFactor  Number of functionally equivalent resources to map to a single
shared resource. The default is 0. See also Resource 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, set Architecture to
MATLAB Datapath . See also Streaming (HDL Coder). 
UseMatrixTypesInHDL  Generate 2D matrices in HDL code. The default is
off . To specify this
property, set Architecture to
MATLAB Function . See also UseMatrixTypesInHDL (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, set
Architecture to MATLAB
Function . 
Restrictions
If the block contains a System
object™, block inputs cannot have nondiscrete (constant or
Inf
) sample time.
HDL Coder does not support a MATLAB Function that
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 a
MATLAB Function block, see Supported MATLAB Data Types, Operators, and Control Flow Statements (HDL Coder).
PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.
FixedPoint Conversion
Design and simulate fixedpoint systems using FixedPoint Designer™.
Actual data type or capability support depends on block implementation.
Introduced in R2011a