Function Definition
There are special considerations when you create MATLAB® functions that are intended for use in a MATLAB
Function block. These include certain restrictions when you use
varargin
and varargout
, recursive
functions, anonymous functions, and nested functions. To generate code for
MATLAB recursive functions, the code generator uses either compile-time
recursion or run-time recursion. To produce efficient C/C++ code, the code
generator sometimes creates multiple versions of a function, referred to as
function specializations. In certain cases, you can prevent the code generator
from creating function specializations by using coder.ignoreSize
and coder.ignoreConst
. You can bypass
code generation for a specific function in your MATLAB code and instead use the MATLAB engine to execute the call. To do this, use the coder.extrinsic
construct.
Blocks
MATLAB Function | Include MATLAB code in Simulink models |
MATLAB System | Include System object in model |
Functions
coder.extrinsic | Declare function as extrinsic and execute it in MATLAB |
coder.ignoreSize | Prevent code generator from creating function specializations for constant-size expressions |
coder.ignoreConst | Prevent use of constant value of expression for function specializations |
coder.mustBeConst | Validate that value is a compile-time constant (Since R2023b) |
coder.unroll | Unroll for -loop by making a copy of
the loop body for each loop iteration |
coder.sameSizeBinaryOp | Apply element-wise binary operations without implicit expansion (Since R2021b) |
coder.noImplicitExpansionInFunction | Disable implicit expansion within the specified function in the generated code (Since R2021b) |
coder.read | Read data files at run time in generated code (Since R2023a) |
coder.write | Create data files that the generated code reads at run time (Since R2023a) |
Topics
- Compilation Directive %#codegen
Indicate that a MATLAB function is intended for code generation.
- Generate Code With Implicit Expansion Enabled
The code generator introduces modifications in the generated code to accomplish implicit expansion.
- Optimize Implicit Expansion in Generated Code
Implicit expansion in the generated code is enabled by default.
- Code Generation for Variable Length Argument Lists
Generate code for
varargin
andvarargout
. - Generate Code for arguments Block That Validates Input and Output Arguments
Generate code for MATLAB code that constrains class, size, and other aspects of function input and output values.
- Code Generation for Recursive Functions
Use recursive functions in MATLAB code that is intended for code generation.
- Force Code Generator to Use Run-Time Recursion
Rewrite your MATLAB code so that the code generator uses run-time recursion instead of compile-time recursion.
- Code Generation for Anonymous Functions
Use anonymous functions in MATLAB code intended for code generation.
- Code Generation for Nested Functions
Use nested functions in MATLAB code intended for code generation.
- Resolution of Function Calls for Code Generation
The code generator uses precedence rules to resolve function calls.
- Resolution of File Types on Code Generation Path
The code generator uses precedence rules to resolve file types.
- Use MATLAB Engine to Execute a Function Call in MATLAB Function Blocks
If a function is not supported for code generation, declare it as extrinsic to execute in MATLAB.
Troubleshooting
Nonconstant Index into varargin or varargout in a for-Loop
Force loop unrolling when the code generator cannot
determine the value of the index into varargin
or varargout
.
Avoid Duplicate Functions in Generated Code
Reduce the occurrence of duplicate functions in the generated code.
Output Variable Must Be Assigned Before Run-Time Recursive Call
Troubleshoot output variable assignment for run-time recursion.
Compile-Time Recursion Limit Reached
Troubleshoot compile-time recursion limit error.
Resolve Error: Size Mismatches
Troubleshoot size mismatch errors that occur during code generation.