Main Content

Function Definition

R2026b
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 extrinsic function
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
coder.noImplicitExpansionInFunctionDisable implicit expansion within the specified function in the generated code
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.

Extrinsic Functions

Using Extrinsic Functions (MATLAB Coder)

Use MATLAB to execute a function call in the generated code.

Working With mxArray Outputs in Generated Code (MATLAB Coder)

Handle the mxArray objects returned by extrinsic functions.

Use Extrinsic Functions to Calibrate Data for Standalone Code Generation (MATLAB Coder)

Use MATLAB to execute function calls in the generated code.

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.

Effects of Implicit Expansion on Generated Code (MATLAB Coder)

Implicit expansion in the generated code can affect output sizes, code size, and performance.

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.

Troubleshoot Replicated Functions in Generated Code (MATLAB Coder)

Troubleshoot duplicate functions in the generated code.

Resolve Error: Output Variable Must Be Assigned Before Run-Time Recursive Call (MATLAB Coder)

Troubleshoot error when the MATLAB code does not assign a value to an output variable before a recursive call.

Resolve Error: Compile-Time Recursion Limit Reached (MATLAB Coder)

Troubleshoot error when recursion during code generation exceeds the limit.

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.