Code and Integration Limitations for MATLAB Function Blocks
Because MATLAB Function blocks rely on MATLAB® Coder™ and Stateflow® infrastructure, you must:
Declare functions as extrinsic to use functions that are not supported for code generation.
Follow the data definition guidelines.
Set blocks to use nondirect feedthrough to use the block in a feedback loop.
Additionally, after you assign properties to a variable, you cannot redefine its class, size, or complexity elsewhere in the function body, with some exceptions.
Use Supported Functions
When you simulate a model that contains a MATLAB Function block, the software generates binary code or C/C++ MATLAB executable (MEX) code from the block and integrates this code with the model. The MATLAB Function block uses the same infrastructure as MATLAB Coder, which you use to generate C/C++ code from MATLAB code outside of Simulink®.
Because the MATLAB Function block relies on code generation technology to compile and simulate, C/C++ code generation limitations for MATLAB Coder also apply to MATLAB Function blocks. As a result, you can only use MATLAB language features that are supported for C/C++ code generation in MATLAB Function block code if you declare them as extrinsic. For a list of functions supported for code generation, see Functions and Objects Supported for C/C++ Code Generation. For supported language features, see MATLAB Language Features Supported for C/C++ Code Generation.
Use Extrinsic Functions
You can still use the functions that are not supported in simulation by declaring them
as extrinsic with
coder.extrinsic. Functions that you
declare as extrinsic execute in the MATLAB workspace during model simulation.
For example, if you want to use
patch to plot data with a MATLAB Function block, define the
function as an extrinsic function by using
The MATLAB Function block automatically treats some plotting functions,
axis, as extrinsic. You do not
have to explicitly declare them as extrinsic functions by using
Extrinsic functions are omitted from generated standalone code. Therefore, do not use extrinsic functions when generating standalone code if they affect block outputs. For more information, see Resolution of Function Calls for Code Generation and Use the coder.extrinsic Construct.
Prepare Variables for Code Generation
To generate efficient standalone code, you must define data types differently than you normally would when running your code in MATLAB. See Data Definition Considerations for Code Generation.
After you assign properties to a variable, you cannot redefine its class, size, or complexity elsewhere in the function body, with some exceptions. See Reassignment of Variable Properties.
Use Nondirect Feedthrough in a MATLAB Function Block
In Simulink blocks, direct feedthrough means that the value of an input port signal directly controls the output of a block. In nondirect feedthrough, the value of the output signal does not depend on the value of the input signal in at least one function during the simulation.
By default, MATLAB Function blocks enable direct feedthrough. If you disable direct feedthrough, the Simulink semantics 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.
To use nondirect feedthrough:
Limitations of Nondirect Feedthrough
When Allow direct feedthrough is cleared, do not program outputs to rely on inputs or updated persistent variables. For example, do not use this:
counter = counter + 1; % update state output = counter; % compute output based on updated state
output = counter; % compute output based on current state counter = counter + 1; % update state
You also cannot call custom code functions in MATLAB Function blocks because it is not possible to enforce that the outputs of custom code functions depend only on the current state. Calling a custom code function in a MATLAB Function block triggers an error.
Additionally, MATLAB Function blocks with function call outputs can cause unexpected results. When Allow direct feedthrough is disabled, the triggered subsystem receives function calls during the model update phase, and therefore the function call output executes later than required. To avoid these issues, enable Allow direct feedthrough when using function call outputs.