MATLAB Function
Include MATLAB code in models that generate embeddable C code
Libraries:
Simulink /
UserDefined Functions
HDL Coder /
UserDefined Functions
Description
With a MATLAB Function block, you can write a MATLAB^{®} function for use in a Simulink^{®} model. The MATLAB function executes for simulation and generates code for a Simulink Coder™ target. If you are new to Simulink and MATLAB products, see Implement MATLAB Functions in Simulink with MATLAB Function Blocks for an overview.
Doubleclicking the MATLAB Function block opens the MATLAB Function Block Editor, where you write the MATLAB function. The example model call_stats_block2
discussed
in Implement MATLAB Functions in Simulink with MATLAB Function Blocks uses
the following function in the MATLAB Function Block Editor:
function [mean,stdev] = stats(vals) % Calculates a statistical mean and a standard % deviation for the values in vals. len = length(vals); mean = avg(vals,len); stdev = sqrt(sum(((valsavg(vals,len)).^2))/len); plot(vals,"+"); function mean = avg(array,size) mean = sum(array)/size;
The function specifies the input and output data in the function declaration statement
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
call_stats_block2
model.
You can also define variables, add an input trigger, and create function call outputs by using the Model Explorer or the Symbols pane. For more information, see Create and Define MATLAB Function Block Variables, Manage the Input Trigger of a MATLAB Function Block, and Manage Function Call Outputs of a MATLAB Function Block.
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:
The first assignment to a variable defines its, size, class, and complexity.
See Best Practices for Defining Variables for C/C++ Code Generation.
You cannot reassign variable properties after the initial assignment except when using variablesize data or reusing variables in the code for different purposes.
In addition to language restrictions, the MATLAB Function block supports a subset of the functions available in MATLAB. These functions include functions in common categories, such as:
For more information, see Functions and Objects Supported for C/C++ Code Generation.
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
New MATLAB Function blocks do not include the
%#codegen
directive, but check for errors as if it is
included. Adding the %#codegen
directive to a MATLAB
Function block does not affect error checking. 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 Use MATLAB Engine to Execute a Function Call in Generated Code 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 Symbols pane, 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 Define and Modify Variable Data Types, Specify Size of MATLAB Function Block Variables, and Configure MATLAB Function Block Parameter Variables 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, clear the Allow direct feedthrough property. Nondirect feedthrough semantics ensure that outputs rely only on the current state. Using nondirect feedthrough enables you to use MATLAB Function blocks in a feedback loop and prevent algebraic loops. For more information, see Use Nondirect Feedthrough in a MATLAB Function Block.
Examples
Ports
Input
Output
Parameters
Block Characteristics
Data Types 

Direct Feedthrough 

Multidimensional Signals 

VariableSize Signals 

ZeroCrossing Detection 

^{a} Actual data type or capability support depends on block implementation. 