Main Content

coder.typeof

Create coder.Type object to represent the type of an entry-point function input

Description

Note

You can also create and edit coder.Type objects interactively by using the Coder Type Editor. See Create and Edit Input Types by Using the Coder Type Editor.

type_obj = coder.typeof(v) creates an object that is derived from coder.Type to represent the type of v for code generation. Use coder.typeof to specify only input parameter types. For example, use it with the fiaccel function -args option. Do not use it in MATLAB® code from which you intend to generate a MEX function.

example

type_obj = coder.typeof(v,sz,variable_dims) returns a modified copy of type_obj = coder.typeof(v) with upper bound size specified by sz and variable dimensions specified by variable_dims.

example

type_obj = coder.typeof(v,'Gpu', true) creates an object that is derived from coder.Type to represent v as a GPU input type for code generation. This option requires a valid GPU Coder™ license.

type_obj = coder.typeof(type_obj) returns type_obj itself.

example

Examples

collapse all

Create a type for a simple fixed-size 5x6 matrix of doubles.

coder.typeof(ones(5,6))
ans = 

coder.PrimitiveType
   5×6 double

coder.typeof(0,[5 6])
ans = 

coder.PrimitiveType
   5×6 double

Create a type for a variable-size matrix of doubles.

coder.typeof(ones(3,3),[],1)
ans = 

coder.PrimitiveType
   :3×:3 double
% ':' indicates variable-size dimensions

Create a type for a matrix with fixed-size and variable-size dimensions.

coder.typeof(0,[2,3,4],[1 0 1])
ans = 

coder.PrimitiveType
   :2×3×:4 double

coder.typeof(10,[1 5],1) 
ans = 

coder.PrimitiveType
   1×:5 double
% ':' indicates variable-size dimensions

Create a type for a matrix of doubles, first dimension unbounded, second dimension with fixed size.

coder.typeof(10,[inf,3]) 
ans = 

coder.PrimitiveType
   :inf×3 double
% ':' indicates variable-size dimensions

Create a type for a matrix of doubles, first dimension unbounded, second dimension with variable size that has an upper bound of 3.

coder.typeof(10,[inf,3],[0 1]) 
ans = 

coder.PrimitiveType
   :inf×:3 double

Convert a fixed-size matrix to a variable-size matrix.

coder.typeof(ones(5,5),[],1) 
 ans = 

coder.PrimitiveType
   :5×:5 double
% ':' indicates variable-size dimensions

Create a type for a structure with a variable-size field.

x.a = coder.typeof(0,[3 5],1);
x.b = magic(3);
coder.typeof(x)
ans = 

coder.StructType
   1×1 struct
      a: :3×:5 double
      b: 3×3 double
% ':' indicates variable-size dimensions

Create a nested structure (a structure as a field of another structure).

S = struct('a',double(0),'b',single(0));
SuperS.x = coder.typeof(S);
SuperS.y = single(0);
coder.typeof(SuperS)  
ans = 

coder.StructType
   1×1 struct
      x: 1×1 struct
         a: 1×1 double
         b: 1×1 single
      y: 1×1 single

Create a structure containing a variable-size array of structures as a field.

S = struct('a',double(0),'b',single(0));
SuperS.x = coder.typeof(S,[1 inf],[0 1]);
SuperS.y = single(0);
coder.typeof(SuperS)
ans = 

coder.StructType
   1×1 struct
      x: 1×:inf struct
         a: 1×1 double
         b: 1×1 single
      y: 1×1 single
% ':' indicates variable-size dimensions

Create a type for a homogeneous cell array with a variable-size field.

a = coder.typeof(0,[3 5],1);
b = magic(3);
coder.typeof({a b})
ans = 

coder.CellType
   1×2 homogeneous cell 
      base: :3×:5 double
% ':' indicates variable-size dimensions

Create a type for a heterogeneous cell array.

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

coder.CellType
   1×2 heterogeneous cell 
      f1: 1×1 char
      f2: 1×1 double

Create a variable-size homogeneous cell array type from a cell array that has the same class but different sizes.

1. Create a type for a cell array that contains two character vectors with different sizes. The cell array type is heterogeneous.

coder.typeof({'aa','bbb'})
ans = 

coder.CellType
   1×2 heterogeneous cell 
      f1: 1×2 char
      f2: 1×3 char

2. Create a type by using the same cell array input. This time, specify that the cell array type has variable-size dimensions. The cell array type is homogeneous.

coder.typeof({'aa','bbb'},[1,10],[0,1])
ans = 

coder.CellType
   1×:10 locked homogeneous cell 
      base: 1×:3 char
% ':' indicates variable-size dimensions

Change a fixed-size array to a bounded, variable-size array.

Create a type for a value class object.

1. Create this value class:

classdef mySquare
    properties
        side;
    end
    methods
        function obj = mySquare(val)
            if nargin > 0
                obj.side = val;
            end
        end
        function a = calcarea(obj)
            a = obj.side * obj.side;
        end
    end
end

2. Create an object of mySquare.

sq_obj = coder.typeof(mySquare(4))
sq_obj = 

coder.ClassType
   1×1 mySquare   
      side: 1×1 double

3. Create a type for an object that has the same properties as sq_obj.

t = coder.typeof(sq_obj)
t = 

coder.ClassType
   1×1 mySquare   
      side: 1×1 double

Alternatively, you can create the type from the class definition:

t = coder.typeof(mySquare(4))
t = 

coder.ClassType
   1×1 mySquare   
      side: 1×1 double

Define a string scalar. For example:

s = "mystring";

Create a type from s.

t = coder.typeof(s);

Assign the StringLength property of the type object the upper bound of the string length and set VariableStringLength to true. Specify that type object t is variable-size with an upper bound of 10.

t.StringLength = 10;
t.VariableStringLength = true;

To specify that t is variable-size without an upper bound:

t.StringLength = Inf;
This automatically sets the VariableStringLength property to true.

Pass the type to codegen by using the -args option.

codegen myFunction -args {t}

Input Arguments

collapse all

v can be a MATLAB numeric, logical, char, enumeration, or fixed-point array. v can also be a cell array, structure, or value class that contains the previous types.

When v is a cell array whose elements have the same classes but different sizes, if you specify variable-size dimensions, coder.typeof creates a homogeneous cell array type. If the elements have different classes, coder.typeof reports an error.

Example: coder.typeof(ones(5,6));

Data Types: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Size vector specifying each dimension of type object.

If sz specifies inf for a dimension, then the size of the dimension is unbounded and the dimension is variable size. When sz is [], the upper bounds of v do not change.

If size is not specified, sz takes the default dimension of v.

Example: coder.typeof(0,[5,6]);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Logical vector that specifies whether each dimension is variable size (true) or fixed size (false). For a cell array, if the elements have different classes, you cannot specify variable-size dimensions.

If you do not specify the variable_dims input parameter, the bounded dimensions of the type are fixed.

A scalar variable_dims applies to all dimensions. However, if variable_dims is 1, the size of a singleton dimension remains fixed.

Example: coder.typeof(0,[2,3,4],[1 0 1]);

Data Types: logical

coder.Type object to represent the type of v for code generation.

Example: type_obj = coder.typeof(ones(5,6));

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Output Arguments

collapse all

coder.Type object to represent the type of v for code generation.

Example: type_obj = coder.typeof(ones(5,6));

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Limitations

  • For sparse matrices, coder.typeof drops upper bounds for variable-size dimensions.

  • Scalar GPU arrays, structures, cell-arrays, classes, enumerated types, character, half-precision and fixed-point data types are not supported.

  • When using coder.typeof to represent GPU arrays, the memory allocation (malloc) mode property of the GPU code configuration object must be set to be 'discrete'.

  • You cannot pass an unconfigured dictionary to coder.typeof.

Tips

  • coder.typeof fixes the size of a singleton dimension unless the variable_dims argument explicitly specifies that the singleton dimension has a variable size.

    For example, the following code specifies a 1-by-:10 double. The first dimension (the singleton dimension) has a fixed size. The second dimension has a variable size.

    t = coder.typeof(5,[1 10],1)
    By contrast, this code specifies a :1-by-:10 double. Both dimensions have a variable size.
    t = coder.typeof(5,[1 10],[1 1])

    Note

    For a MATLAB Function block, singleton dimensions of input or output signals cannot have a variable size.

  • If you are already specifying the type of an input variable by using a type function, do not use coder.typeof unless you also want to specify the size. For instance, instead of coder.typeof(single(0)), use the syntax single(0).

  • For cell array types, 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, classification as homogeneous or heterogeneous is ambiguous. For example, the type for {1 [2 3]} can be a 1x2 heterogeneous type where 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 coder.CellType makeHomogeneous or makeHeterogeneous methods to make a type with the classification that you want. 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 heterogeneous and homogeneous. You cannot later use one of these methods to create a copy that has a different classification.

  • You can use coder.typeof to create an unbounded GPU array input and pass it to the entry-point functions. For example, create a GPU input using coder.typeof(int32(1),[inf,1],'Gpu',true).

  • During code generation with GPU array types, if one input to the entry-point function is of the GPU array type, then the output variables are all GPU array types, provided they are supported for GPU code generation. For example. if the entry-point function returns a struct and because struct is not supported, the generated code returns a CPU output. However, if a supported matrix type is returned, then the generated code returns a GPU output.

Version History

Introduced in R2011a

expand all