Main Content

Dynamic memory allocation in MATLAB functions

Option to use dynamic memory allocation for MATLAB code

Model Configuration Pane: Simulation Target

Description

The Dynamic memory allocation in MATLAB functions parameter specifies whether to use dynamic memory allocation for MATLAB® code in MATLAB Function blocks, Stateflow® charts, and MATLAB System blocks in simulation and for code generation.

Use dynamic memory allocation for variable-size arrays whose size, in bytes, is greater than or equal to the dynamic memory allocation threshold. This parameter applies to MATLAB code in a MATLAB Function block, a Stateflow chart, or a System object™ associated with a MATLAB System block. This parameter applies to the model during simulation and code generation. This parameter does not apply to:

  • Input signals

  • Output signals

  • Parameters

  • Global variables

  • Discrete state properties of the System object associated with a MATLAB System block

Code that uses dynamic memory allocation can be less efficient than code that uses static memory allocation. Unless your model requires dynamic memory allocation, clear this parameter.

If sufficient memory is not available for a dynamic memory allocation request, dynamic memory allocation can fail. The code generator does not check memory allocation requirements. For safety-critical applications, the recommended setting for this parameter is off.

Settings

on | off
on

The software dynamically allocates memory for variable-size arrays in MATLAB code that have a size in bytes greater than the dynamic memory allocation threshold specified by the Dynamic memory allocation threshold in MATLAB functions parameter.

This parameter is selected by default for GRT-based code generation targets.

off

The software does not use dynamic memory allocation for MATLAB code.

This parameter is cleared by default for ERT-based code generation targets.

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityNo impact
Efficiencyoff
Safety precautionoff

Programmatic Use

Parameter: MATLABDynamicMemAlloc
Type: string | character vector
Values: 'on' | 'off'
Default: 'on'

Limitations

  • If you generate code using GPU Coder™, the generated code allocates memory on the GPU using the cudaMalloc API. As a result, the generated code might dynamically allocate GPU memory for MATLAB functions even if this parameter is set to off.

Version History

Introduced in R2017a