Main Content

coder.newtype

Createcoder.Typeobject to represent type of an entry-point function input

Description

Thecoder.newtypefunction is an advanced function that you can use to control thecoder.Typeobject. Consider usingcoder.typeofinstead ofcoder.newtype. The functioncoder.typeofcreates a type from a MATLAB®example. By default,t=coder.newtype('class_name') does not assign any properties of the class,class_nameto the objectt.

example

Note

You can also create and editcoder.Typeobjects interactively by using the Coder Type Editor. SeeCreate and Edit Input Types by Using the Coder Type Editor.

t= coder.newtype(numeric_class,sz,variable_dims)creates acoder.Typeobject representing values of classnumeric_class, sizessz(upper bound), and variable dimensionsvariable_dims. Ifszspecifiesinffor a dimension, then the size of the dimension is unbounded and the dimension is variable-size. Whenvariable_dimsis not specified, the dimensions of the type are fixed except for those that are unbounded. Whenvariable_dimsis a scalar, it is applied to type dimensions that are not1or0, which are fixed.

t= coder.newtype(numeric_class,sz,variable_dims,Name,Value)creates acoder.Typeobject by using additional options specified as one or more Name, Value pair arguments.

example

t= coder.newtype('constant',value)creates acoder.Constantobject representing a single value. Use this type to specify a value that must be treated as a constant in the generated code.

example

t= coder.newtype('struct',struct_fields,sz,variable_dims)creates acoder.StructTypeobject for an array of structures that has the same fields as the scalar structurestruct_fields. The structure array type has the size specified byszand variable-size dimensions specified byvariable_dims.

example

t= coder.newtype('cell',cells,sz,variable_dims)creates acoder.CellTypeobject for a cell array that has the cells and cell types specified bycells. The cell array type has the size specified byszand variable-size dimensions specified byvariable_dims. You cannot change the number of cells or specify variable-size dimensions for a heterogeneous cell array.

example

t= coder.newtype('embedded.fi',numerictype,sz,variable_dims,Name,Value)creates acoder.FiTypeobject representing a set of fixed-point values that havenumerictypeand additional options specified by one or more Name, Value pair arguments.

example

t= coder.newtype(enum_value,sz,variable_dims)creates acoder.Typeobject representing a set of enumeration values of classenum_value.

example

t= coder.newtype('class_name')creates acoder.ClassTypeobject for an object of the classclass_name. The new object does not have any properties of the classclass_name.

example

t= coder.newtype('string')creates a type 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, create a type for the character vector and assign it to theValueproperty of the string scalar type. For example,t.Properties.Value = coder.newtype('char',[1 10],[0 1])specifies that the character vector inside the string scalar is variable-size with an upper bound of 10.

Examples

collapse all

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)
t= coder.Constant 42

Create acoder.EnumTypeobject by using the name of an existing MATLAB enumeration.

1. Define an enumerationMyColors. On the MATLAB path, create a file named MyColors containing:

classdefMyColors < int32enumerationgreen(1), red(2),endend

2. Create acoder.EnumTypeobject 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:

classdefmySquarepropertiesside;endmethodsfunctionobj = mySquare(val)ifnargin > 0 obj.side = val;endendfunctiona = calcarea(obj) a = obj.side * obj.side;endendend

2. Create a type for an object that has the same properties asmySquare.

t= coder.newtype('mySquare');

3. The previous step creates acoder.ClassTypetype fort, but does not assign any properties ofmySquareto it. To ensurethas all the properties ofmySquare, change the type of the propertysideby usingt.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.

t.Properties.Value = coder.newtype('char',[1,10]);

3. Make the string variable-size with an upper bound of 10.

t.Properties.Value = coder.newtype('char',[1,10],[0,1]);

4. Make the string variable-size with no upper bound.

t.Properties.Value = coder.newtype('char',[1,inf]);

Input Arguments

collapse all

Class of the set of values represented by the type object.

Example:coder.newtype('double',[6,3]);

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

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 ofcoder.Typeobjects 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. Theszdimension 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 thecoder.ClassTypeis created. Specify as a character vector or string scalar.class_namemust be the name of a value class.

Example:coder.newtype('mySquare')

Data Types:char|string

The value ofvariable_dimsistruefor dimensions for whichszspecifies an upper bound ofinf;falsefor 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 comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:coder.newtype('embedded.fi',numerictype(1,16,15),[1 2])

Setcomplextotrueto create acoder.Typeobject that can represent complex values. The type must support complex data.

Specify localfimath. If you do not specifyfimath, the code generator uses default fimath values.

Use with only

t= coder.newtype('embedded.fi',numerictype,sz,variable_dims,Name,Value)

Setsparsetotrueto create acoder.Typeobject representing sparse data. The type must support sparse data.

Not for use with

t= coder.newtype('embedded.fi',numerictype,sz,variable_dims,Name,Value)

Setgputotrueto create acoder.Typeobject that can represent the GPU input type. This option requires GPU Coder™.

Output Arguments

collapse all

A newcoder.Typeobject.

Limitations

  • For sparse matrices,coder.newtypedrops 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 usingcoder.newtypeto represent GPU inputs, the memory allocation (malloc) mode property of the GPU code configuration object to'discrete'.

Tips

  • Thecoder.newtypefunction fixes the size of a singleton dimension unless thevariable_dimsargument explicitly specifies that the singleton dimension has a variable size.

    例如,这代码指定了一个1 - 10双. 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 aMATLAB Functionblock, singleton dimensions of input or output signals cannot have a variable size.

Alternatives

coder.typeof

Introduced in R2011a