Main Content

isHeterogeneous

Class: coder.CellType
Package: coder

Determine whether cell array type represents a heterogeneous cell array

Syntax

tf = isHeterogeneous(t)

Description

tf = isHeterogeneous(t) returns true if the coder.CellType object t is heterogeneous. Otherwise, it returns false.

Examples

expand all

Create a coder.CellType object for a cell array whose elements have different classes.

t = coder.typeof({'a', 1})
t = 

coder.CellType
   1x2 heterogeneous cell 
      f0: 1x1 char
      f1: 1x1 double

Determine whether the coder.CellType object represents a heterogeneous cell array.

isHeterogeneous(t)
ans =

     1

Write a function assign_name. If the input type t is heterogeneous, the function returns a copy of t. The copy specifies the name for the structure type that represents the cell array type in the generated code.

function ts = assign_name(t, str_name)
assert(isHeterogeneous(t));
ts = coder.cstructname(t, str_name);
disp ts
end

Create a homogeneous type tc.

tc = coder.typeof({1 2 3});

Pass tc to make_varsize.

tc1 = assign_name(tc, 'myname')

The assertions fails because tc is not heterogeneous.

Create a heterogeneous type tc.

tc = coder.typeof({'a' 1});

Pass tc to make_varsize.

tc1 = assign_name(tc, 'myname')
tc1 = 

coder.CellType
   1x2 heterogeneous cell myname
      f0: 1x1 char
      f1: 1x1 double

Tips

  • coder.typeof determines whether the cell array type is homogeneous or heterogeneous. If the cell array elements have the same class and size, coder.typeof returns a homogeneous cell array type. If the elements have different classes, coder.typeof returns a heterogeneous cell array type. For some cell arrays, the classification as homogeneous or heterogeneous is ambiguous. For example, the type for {1 [2 3]} can be a 1x2 heterogeneous type. The first element is double and the second element is 1x2 double. The type can also be a 1x3 homogeneous type in which the elements have class double and size 1x:2. For these ambiguous cases, coder.typeof uses heuristics to classify the type as homogeneous or heterogeneous. If you want a different classification, use the makeHomogeneous or makeHeterogeneous methods. The makeHomogeneous method makes a homogeneous copy of a type. The makeHeterogeneous method makes a heterogeneous copy of a type.

    The makeHomogeneous and makeHeterogeneous methods permanently assign the classification as homogeneous and heterogeneous, respectively. You cannot later use one of these methods to create a copy that has a different classification.

Introduced in R2015b