Main Content

Code Generation for Cell Arrays

When you generate code from MATLAB® code that contains cell arrays, the code generator classifies the cell arrays as homogeneous or heterogeneous. This classification determines how a cell array is represented in the generated code. It also determines how you can use the cell array in MATLAB code from which you generate code.

When you use cell arrays in MATLAB code that is intended for code generation, you must adhere to certain restrictions. See Cell Array Limitations for Code Generation.

Homogeneous vs. Heterogeneous Cell Arrays

A homogeneous cell array has these characteristics:

  • The cell array is represented as an array in the generated code.

  • All elements have the same properties. The type associated with the cell array specifies the properties of all elements rather than the properties of individual elements.

  • The cell array can be variable-size.

  • You can index into the cell array with an index whose value is determined at run time.

A heterogeneous cell array has these characteristics:

  • The cell array is represented as a structure in the generated code. Each element is represented as a field of the structure.

  • The elements can have different properties. The type associated with the cell array specifies the properties of each element individually.

  • The cell array cannot be variable-size.

  • You must index into the cell array with a constant index or with for-loops that have constant bounds.

The code generator uses heuristics to determine the classification of a cell array as homogeneous or heterogeneous. It considers the properties (class, size, complexity) of the elements and other factors, such as how you use the cell array in your program. Depending on how you use a cell array, the code generator can classify a cell array as homogeneous in one case and heterogeneous in another case. For example, consider the cell array {1 [2 3]}. The code generator can classify this cell array as a heterogeneous 1-by-2 cell array. The first element is double scalar. The second element is a 1-by-2 array of doubles. However, if you index into this cell array with an index whose value is determined at run time, the code generator classifies it as a homogeneous cell array. The elements are variable-size arrays of doubles with an upper bound of 2.

Controlling Whether a Cell Array Is Homogeneous or Heterogeneous

For cell arrays with certain characteristics, you cannot control the classification as homogeneous or heterogeneous:

  • If the elements have different classes, the cell array must be heterogeneous.

  • If the cell array is variable-size, it must be homogeneous.

  • If you index into the cell array with an index whose value is determined at run time, the cell array must be homogeneous.

For other cell arrays, you can control the classification as homogeneous or heterogeneous.

Cell Arrays in Reports

To see whether a cell array is homogeneous or heterogeneous, view the variable in the MATLAB Function report.

For a homogeneous cell array, the report has one entry that specifies the properties of all elements. The notation {:} indicates that all elements of the cell array have the same properties.

This image shows a homogenous cell array in a report. the notation {:} appears below the array c.

For a heterogeneous cell array, the report has an entry for each element. For example, for a heterogeneous cell array c with two elements, the entry for c{1} shows the properties for the first element. The entry for c{2} shows the properties for the second element.

This image shows a heterogeneous cell array in a report. {1} and {2} appear below the array c to indicate the different properties of each element.

See Also

|

Related Topics