Main Content

Function Definition

Defining and calling for code acceleration

There are special considerations when you create MATLAB® functions that are intended for fixed-point acceleration. 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 using coder.ignoreConst (MATLAB Coder). While generating a MEX function, 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.

Functions

coder.extrinsicDeclare function as extrinsic and execute it in MATLAB
coder.ignoreConstPrevent use of constant value of expression for function specializations
coder.mustBeConstValidate that value is a compile-time constant (Since R2023b)
coder.unrollUnroll for-loop by making a copy of the loop body for each loop iteration
coder.sameSizeBinaryOpApply element-wise binary operations without implicit expansion (Since R2021b)
coder.noImplicitExpansionInFunctionDisable implicit expansion within the specified function in the generated code (Since R2021b)
coder.readRead data files at run time in generated code (Since R2023a)
coder.writeCreate data files that the generated code reads at run time (Since R2023a)

Variable-Sizing Basics

Generate Code for Variable-Size Arrays (MATLAB Coder)

Learn how the code generator defines and handles fixed- and variable-size data.

Code Generation for Arrays That Grow Via end+1 Indexing (MATLAB Coder)

Code generation considerations when you use (end+1) or {end+1} to grow an array.

Specify Upper Bounds for Variable-Size Arrays in Generated Code (MATLAB Coder)

To avoid dynamic memory allocation, specify upper bounds for variable-size arrays.

Incompatibilities with MATLAB in Variable-Size Support for Code Generation (MATLAB Coder)

Understand how the behavior of the generated code can differ from MATLAB as a result of variable-size data.

Generate Code for a MATLAB Function That Expands a Vector in a Loop (MATLAB Coder)

Generate C and C++ code for a function that uses variable-size data with and without dynamically allocated memory.

Dynamic Memory Allocation

Control Dynamic Memory Allocation in Generated Code (MATLAB Coder)

Balance memory usage and performance by controlling dynamic memory allocation for fixed- and variable-size arrays.

Generate Code with Implicit Expansion Enabled (MATLAB Coder)

The code generator introduces modifications in the generated code to accomplish implicit expansion.

Optimize Implicit Expansion in Generated Code (MATLAB Coder)

Implicit expansion in the generated code is enabled by default.

Representation of Arrays in Generated Code (MATLAB Coder)

Understand how arrays are represented in the generated code.

Use Dynamically Allocated C Arrays in the Generated Function Interfaces (MATLAB Coder)

Understand and use dynamically allocated arrays from the generated C function interfaces.

Use Dynamically Allocated C++ Arrays in Generated Function Interfaces (MATLAB Coder)

Understand and use dynamically allocated arrays from the generated C++ function interfaces.

Troubleshooting

Resolve Error: Arrays Have Incompatible Sizes (MATLAB Coder)

Troubleshoot size incompatibility errors that occur during code generation.

Resolve Error: Fixed Size on the Left Side but Variable Size on the Right (MATLAB Coder)

Troubleshoot errors when assigning variable-size arrays to fixed-size arrays.

Resolve Error: coder.varsize Not Supported for Strings (MATLAB Coder)

Troubleshoot code generation error when using coder.varsize with string variables.

Resolve Error: coder.varsize Not Supported for Class Properties (MATLAB Coder)

Troubleshoot code generation error when using coder.varsize with properties of a MATLAB class.

Resolve Error: Unable to Produce Column Vector from Scalar (MATLAB Coder)

Troubleshoot error when an input argument that is a variable-length column vector at code generation time is a scalar at run time.

Resolve Error: Incorrect Size for Expression (MATLAB Coder)

Troubleshoot MEX function error when the size of an array at run time is incompatible with the size of the array in the generated code.