Main Content

Data Definition Considerations for Code Generation

To generate efficient standalone code, you must define the following types and classes of data differently from when you run your code in MATLAB®.

DataType ConsiderationsMore Information
Arrays

Maximum number of elements is restricted.

Array Size Restrictions for Code Generation
Numeric types

Assign numeric type variables a value before using them in operations or returning them as outputs.

Best Practices for Defining Variables for C/C++ Code Generation (MATLAB Coder)
Complex numbers
  • Set complexity of variables at the time of assignment and before first use.

  • Expressions containing a complex number or variable evaluate to a complex result, even if the imaginary part of the result is zero.

Code Generation for Complex Data
Characters and strings
  • Characters are restricted to 8 bits of precision.

  • For code generation, string scalars do not support global variables, indexing with curly braces, missing values, or size changes by using the function coder.varsize.

Variable-Size data

After initial fixed-size assignment to a variable, attempts to grow the variable might cause a compilation error.

Structures
  • Assign fields to structures in the same order on each control path.

  • Assign corresponding fields in the structure array elements with same size, type, and complexity.

Cell arrays
  • Assign all cell array elements before passing the cell array to a function or returning it from a function.

  • Variable-size cell array elements must all have the same size, type, and complexity.

Tables
  • You must specify variable names by using the 'VariableNames' name-value argument when creating tables from input arrays.

  • Limited data type support when you preallocate a table by using the table function and the 'Size' name-value argument.

  • Table indices that specify variables must be compile time constant.

  • You cannot change the size of a table by assignments.

  • You cannot change the VariableNames, RowNames, DimensionNames, or UserData properties of a table after you create it.

Limitations that apply to classes also apply to tables.

Categorical arrays

Categorical arrays do not support these inputs and operations:

  • Arrays of MATLAB objects

  • Sparse matrices

  • Duplicate category names

  • Growth by assignment

  • Adding a category

  • Deleting an element

Limitations that apply to classes also apply to categorical arrays.

Datetime arrays

datetime arrays do not support these inputs and operations:

  • Text inputs

  • The 'Format' name-value argument

  • The 'TimeZone' name-value argument and the 'TimeZone' property

  • Setting time component properties

  • Growth by assignment

  • Deleting an element

Limitations that apply to classes also apply to datetime arrays.

Duration arrays

Duration arrays do not support these inputs and operations:

  • Text inputs

  • Growth by assignment

  • Deleting an element

  • Converting duration values to text by using char,cellstr, or string functions

Limitations that apply to classes also apply to duration arrays.

Timetables

  • You must specify variable names by using the 'VariableNames' name-value argument when creating timetables from input arrays.

  • Limited data type support when you preallocate a table by using the timetable function and the 'Size' name-value argument.

  • Timetable indices that specify variables must be compile time constant.

  • You cannot change the size of a timetable by assignments.

  • You cannot change the VariableNames, DimensionNames, or UserData properties of a timetable after you create it.

  • If you create a regular timetable, and you attempt to set irregular row times, then an error is produced.

  • If you create an irregular timetable, then it remains irregular even if you set its sample rate or time step.

Limitations that apply to classes also apply to timetables.

Enumerated data

Supports integer-based enumerated types only.

Enumerations
MATLAB Classes

  • Before generating code, it is a best practice to test class property validation by running a MEX function over the full range of input values.

  • If a property does not have an explicit initial value, the code generator assumes that it is undefined at the beginning of the constructor. The code generator does not assign an empty matrix as the default.

  • The coder.varsize function is not supported for class properties.

  • If the initial value of a property is an object, then the property must be constant. To make a property constant, declare the Constant attribute in the property block.

Function handles
  • Assigning different function handles to the same variable can cause a compile-time error.

  • You cannot pass function handles to or from entry-point functions or extrinsic functions.

  • You cannot view function handles from the MATLAB Function Block debugger.

Function Handles
Deep learning arrays

dlarrays do not support these inputs and operations:

  • The data format argument must be a compile-time constant

  • Define dlarray variables inside the entry-point function.

  • The input to a dlarray must be fixed-size.

  • Code generation does not support creating a dlarray type object by using the coder.typeof function with upper bound size and variable dimensions specified.

The information in the preceding table is not an exhaustive list of considerations for each data type. See the topics in the More Information column.

Related Topics