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.
[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
.
[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.
Examples
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
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
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
- 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 ofcoder.typeof(single(0))
, use the syntaxsingle(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.CellTypemakeHomogeneous
ormakeHeterogeneous
methods to make a type with the classification that you want. ThemakeHomogeneous
method makes a homogeneous copy of a type. ThemakeHeterogeneous
method makes a heterogeneous copy of a type.
ThemakeHomogeneous
andmakeHeterogeneous
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 usingcoder.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 becausestruct
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
R2024b: Support for unbounded GPU arrays
For representing GPU arrays, both bounded and unbounded numeric and logical base types are supported.