coder.typeof - Create coder.Type object to represent the type

        of an entry-point function input - MATLAB ([original](https://www.mathworks.com/help/coder/ref/coder.typeof.html)) ([raw](?raw))

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

Syntax

Description

[type_obj](#mw%5F30c74b18-bc1e-41a3-b240-1ffd178afdd4) = coder.typeof([v](#mw%5F299427a1-48ad-42ed-890d-4fbcf6eba83d)) creates an object that is derived from coder.Type to represent the type of v for code generation. Usecoder.typeof to specify only input parameter types. Use it with the codegen function-args option or in a MATLAB® Coder™ project when you are defining an input type by providing a sample code. Do not use it in MATLAB code from which you intend to generate code.

example

[type_obj](#mw%5F30c74b18-bc1e-41a3-b240-1ffd178afdd4) = coder.typeof([v](#mw%5F299427a1-48ad-42ed-890d-4fbcf6eba83d),[sz](#mw%5Fa5687aea-c4e1-4698-bc11-e1e23ce8f6c8),[variable_dims](#mw%5Fc09e521a-a565-48d6-8b61-7043ec8d19c1)) returns a modified copy of type_obj = coder.typeof(v) with upper bound size specified bysz and variable dimensions specified byvariable_dims.

example

[type_obj](#mw%5F30c74b18-bc1e-41a3-b240-1ffd178afdd4) = coder.typeof([v](#mw%5F299427a1-48ad-42ed-890d-4fbcf6eba83d),'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](#mw%5F30c74b18-bc1e-41a3-b240-1ffd178afdd4) = coder.typeof([type_obj](#mw%5F30c74b18-bc1e-41a3-b240-1ffd178afdd4)) returns type_obj itself.

example

Examples

collapse all

Create Type for a Matrix

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

ans =

coder.PrimitiveType 5×6 double

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

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.

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 Type for a Structure

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 Type for a Cell Array

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

Create Type for a Value Class Object

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.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

Create Type for a String Scalar

Define a string scalar. For example:

Create a type froms.

Assign the StringLength property of the type object the upper bound of the string length and setVariableStringLength 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:

This automatically sets the VariableStringLength property totrue.

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

codegen myFunction -args {t}

Input Arguments

collapse all

v — Set of values representing input parameter types

numeric array | character vector | string | struct | cell array

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

sz — Dimension of type object

row vector of integer values

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

variable_dims — Variable or fixed dimension

row vector of logical values

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, ifvariable_dims is 1, the size of a singleton dimension remains fixed.

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

Data Types: logical

type_obj — Type object

coder.Type object

coder.Type object to represent the type ofv 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

type_obj — Type object

coder.Type object

coder.Type object to represent the type ofv 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

Tips

Version History

Introduced in R2011a

expand all

R2024b: Support for unbounded GPU arrays

For representing GPU arrays, both bounded and unbounded numeric and logical base types are supported.