coder.newtype - Create coder.Type object to represent type of an entry-point
function input - MATLAB ([original](https://in.mathworks.com/help/coder/ref/coder.newtype.html)) ([raw](?raw))
Create coder.Type
object to represent type of an entry-point function input
Syntax
Description
The coder.newtype
function is an advanced function that you can use to control the coder.Type
object. Consider using coder.typeof instead ofcoder.newtype
. The function coder.typeof
creates a type from a MATLAB® example. By default, t
=coder.newtype
('class_name'
) does not assign any properties of the class,class_name
to the object t
.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype([numeric_class](#mw%5Fa0557768-a949-4d02-99a1-4c6e52f36a74),[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf))
creates a coder.Type
object representing values of class numeric_class
, sizes sz
(upper bound), and variable dimensions variable_dims
. If sz
specifies inf
for a dimension, then the size of the dimension is unbounded and the dimension is variable-size. When variable_dims
is not specified, the dimensions of the type are fixed except for those that are unbounded. When variable_dims
is a scalar, it is applied to type dimensions that are not 1
or 0
, which are fixed.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype([numeric_class](#mw%5Fa0557768-a949-4d02-99a1-4c6e52f36a74),[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf),[Name,Value](#namevaluepairarguments))
creates a coder.Type
object by using additional options specified as one or more Name, Value pair arguments.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype('constant',[value](#mw%5F423b32c7-4ccf-4dbb-b2f7-0d79331c0dcd))
creates a coder.Constant
object representing a single value. Use this type to specify a value that must be treated as a constant in the generated code.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype('struct',[struct_fields](#mw%5F147fdbcb-d6e9-4849-98ae-2259c91ff900),[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf))
creates a coder.StructType
object for an array of structures that has the same fields as the scalar structurestruct_fields
. The structure array type has the size specified by sz
and variable-size dimensions specified byvariable_dims
.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype('cell',[cells](#mw%5Fe3d1bf72-60c1-4e56-9aca-30c04ad96a4a),[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf))
creates a coder.CellType
object for a cell array that has the cells and cell types specified by cells
. The cell array type has the size specified by sz
and variable-size dimensions specified by variable_dims
. You cannot change the number of cells or specify variable-size dimensions for a heterogeneous cell array.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype('embedded.fi',numerictype,[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf),[Name,Value](#namevaluepairarguments))
creates a coder.FiType
object representing a set of fixed-point values that have numerictype
and additional options specified by one or more Name, Value pair arguments.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype([enum_value](#mw%5F2c1d03d6-5e30-4901-be04-93f5de278730),[sz](#mw%5Fbe76bc75-72d4-4ea4-b9db-dcc57d67a254),[variable_dims](#mw%5Fd730f3a8-a0a0-492f-9315-fbb03fe2a0cf))
creates a coder.Type
object representing a set of enumeration values of class enum_value
.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype(['class_name'](#mw%5Fd0cdd930-1eb4-4a11-b2ef-67f6fe567289))
creates a coder.ClassType
object for an object of the class class_name
. The new object does not have any properties of the classclass_name
.
[t](#mw%5F88e73e38-9d22-42d9-9f03-747239a8786a) = coder.newtype('string')
creates acoder.StringType
object for a string scalar. A string scalar contains one piece of text represented as a character vector. To specify the size of the character vector and whether the second dimension is variable-size, set theStringLength
property to the required size and setVariableStringLength
to true
. For example,t.StringLength = 10
and t.VariableStringLength = true
specifies that the string scalar is variable-size with an upper bound of 10.
Examples
Create a type for a variable-size matrix of doubles.
t = coder.newtype('double',[2 3 4],[1 1 0])
t =
coder.PrimitiveType :2×:3×4 double % ':' indicates variable-size dimensions
Create a type for a matrix of doubles, first dimension unbounded, and second dimension with fixed size.
t = coder.newtype('double',[inf,3])
t =
coder.PrimitiveType :inf×3 double
t = coder.newtype('double',[inf,3],[1 0])
% also returns t =
coder.PrimitiveType :inf×3 double % ':' indicates variable-size dimensions
Create a type for a matrix of doubles, first dimension unbounded, and second dimension with variable-size that has an upper bound of3
.
t = coder.newtype('double',[inf,3],[0 1])
t =
coder.PrimitiveType :inf×:3 double
% ':' indicates variable-size dimensions
Create a type for a structure with a variable-size field.
ta = coder.newtype('int8',[1 1]); tb = coder.newtype('double',[1 2],[1 1]); t = coder.newtype('struct',struct('a',ta,'b',tb),[1 1],[1 1])
t =
coder.StructType :1×:1 struct a: 1×1 int8 b: :1×:2 double % ':' indicates variable-size dimensions
Create a type for a heterogeneous cell array.
ta = coder.newtype('int8',[1 1]); tb = coder.newtype('double',[1 2],[1 1]); t = coder.newtype('cell',{ta, tb})
t =
coder.CellType 1×2 heterogeneous cell f1: 1×1 int8 f2: :1×:2 double % ':' indicates variable-size dimensions
Create a type for a homogeneous cell array.
ta = coder.newtype('int8',[1 1]); tb = coder.newtype('int8',[1 2],[1 1]); t = coder.newtype('cell',{ta, tb},[1,1],[1,1])
t =
coder.CellType :1×:1 homogeneous cell base: :1×:2 int8 % ':' indicates variable-size dimensions
Create a new constant type to use in code generation.
t = coder.newtype('constant',42)
Create a coder.EnumType
object by using the name of an existing MATLAB enumeration.
1. Define an enumeration MyColors
. On the MATLAB path, create a file named MyColors containing:
classdef MyColors < int32 enumeration green(1), red(2), end end
2. Create a coder.EnumType
object from this enumeration.
t = coder.newtype('MyColors')
t =
coder.EnumType 1×1 MyColors
Create a fixed-point type for use in code generation.
The fixed-point type uses default fimath values.
t = coder.newtype('embedded.fi',numerictype(1, 16, 15),[1 2])
t =
coder.FiType 1×2 embedded.fi DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 15
Create a type for an object to use in code generation.
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 a type for an object that has the same properties as mySquare
.
t = coder.newtype('mySquare');
3. The previous step creates a coder.ClassType
type for t
, but does not assign any properties ofmySquare
to it. To assign the properties of mySquare
to t
, change the type of the propertyside
by using t.Properties
.
t.Properties.side = coder.typeof(int8(3))
t =
coder.ClassType
1×1 mySquare
side: 1×1 int8
Create a type for a string scalar to use in code generation.
1. Create the string scalar type.
t = coder.newtype('string');
2. Specify the size.
3. Make the string variable-size.
t.VariableStringLength = true;
4. To make the string variable-size with no upper bound, set StringLength
to Inf
.
Note
Setting StringLength
to Inf
implicitly sets VariableStringLength
totrue
.
Input Arguments
Class of the set of values represented by the type object.
Example: coder.newtype('double',[6,3]);
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
Scalar structure used to specify the fields in a new structure type.
Example: coder.newtype('struct',struct('a',ta,'b',tb));
Data Types: struct
Cell array of coder.Type
objects that specify the types of the cells in a new cell array type.
Example: coder.newtype('cell',{ta,tb});
Data Types: cell
Size vector specifying each dimension of type object. Thesz
dimension cannot change the number of cells for a heterogeneous cell array.
Example: coder.newtype('int8',[1 2]);
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Complex Number Support: Yes
Name of the class from which the coder.ClassType
is created. Specify as a character vector or string scalar. class_name
must be the name of a value class.
Example: coder.newtype('mySquare')
Data Types: char
| string
The value of variable_dims
is true
for dimensions for which sz specifies an upper bound ofinf
; false
for all other dimensions.
Logical vector that specifies whether each dimension is variable size (true) or fixed size (false). You cannot specify variable-size dimensions for a heterogeneous cell array.
Example: coder.newtype('char',[1,10],[0,1]);
Data Types: logical
Specifies the actual value of the constant.
Example: coder.newtype('constant',41);
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
Enumeration values of a class.
Example: coder.newtype('MyColors');
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
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: t = coder.newtype('double',[10 20],sparse = true)
Set complex
to true
to create acoder.Type
object that can represent complex values. The type must support complex data.
Specify local fimath
. If you do not specifyfimath
, the code generator uses defaultfimath
values.
Use this option only when you create a coder.FiType
object.
Set sparse
to true
to create acoder.Type
object representing sparse data. The type must support sparse data.
This option is not supported when:
- You create a
coder.FiType
object. - You set the
gpu
option totrue
.
Set gpu
to true
to create acoder.Type
object that can represent the GPU input type. This option requires GPU Coder™.
Output Arguments
Limitations
- For sparse matrices,
coder.newtype
drops upper bounds for variable-size dimensions. - For GPU input types, only bounded numeric and logical base types are supported. Scalar GPU arrays, structures, cell-arrays, classes, enumerated types, character, half-precision and fixed-point data types are not supported.
- When using
coder.newtype
to represent GPU inputs, the memory allocation (malloc) mode property of the GPU code configuration object to'discrete'
.
Tips
- The
coder.newtype
function fixes the size of a singleton dimension unless thevariable_dims
argument explicitly specifies that the singleton dimension has a variable size.
For example, this 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.newtype('double',[1 10],1)
By contrast, this code specifies a :1-by-:10 double. Both dimensions have a variable size.
t = coder.newtype('double',[1 10],[1 1]) - For a MATLAB Function block, singleton dimensions of input or output signals cannot have a variable size.
Version History
Introduced in R2011a