coder.newtype

Create a coder.Type object

Description

example

Note

coder.newtype is an advanced function that you can use to control the coder.Type object. Consider using coder.typeof instead. coder.typeof creates a type from a MATLAB® example.

t= coder.newtype(numeric_class,sz,variable_dims) creates a coder.Type object representing values of class numeric_class with (upper bound) sizes sz 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 dimensions of the type that are not 1 or 0, which are fixed.

t = coder.newtype(numeric_class,sz,variable_dims, Name,Value) creates a coder.Type object with additional options specified by one or more Name, Value pair arguments.

example

t = coder.newtype('constant',value) 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.

example

t = coder.newtype('struct',struct_fields,sz,variable_dims) creates a coder.StructType object for an array of structures that has the same fields as the scalar structure struct_fields. The structure array type has the size specified by sz and variable-size dimensions specified by variable_dims.

example

t = coder.newtype('cell',cells,sz,variable_dims) 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 = coder.newtype('embedded.fi',numerictype,sz,variable_dims, Name,Value) creates a coder.FiType object representing a set of fixed-point values with numerictype and additional options specified by one or more Name, Value pair arguments.

t = coder.newtype(enum_value,sz,variable_dims) creates a coder.Type object representing a set of enumeration values of class enum_value.

t = coder.newtype(class_name) creates a coder.ClassType object for an object of the class class_name.

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 the Value property 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 matrix of doubles for use in code generation.

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, second dimension with fixed-size.

coder.newtype('double',[inf,3]) 
ans = 

coder.PrimitiveType
   :inf×3 double

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

coder.newtype('double', [inf, 3], [1 0])
%  also returns 
ans = 

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

Create a structure type to use in code generation.

ta = coder.newtype('int8',[1 1]);
tb = coder.newtype('double',[1 2],[1 1]);
coder.newtype('struct',struct('a',ta,'b',tb))
ans = 

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

Create a cell array to use in code generation.

ta = coder.newtype('int8',[1 1]);
tb = coder.newtype('double',[1 2],[1 1]);
coder.newtype('cell',{ta, tb})
% returns 1x2 heterogeneous cell
ans = 

coder.CellType
   1×2 heterogeneous cell 
      f1: 1×1 int8
      f2: :1×:2 double
% ':' indicates variable-size dimensions

Create a new constant type to use in code generation.

k = coder.newtype('constant', 42)
k = 

coder.Constant
       42

Create a coder.EnumType object 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');

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
    
    end
    function a = calcarea(obj)
    a = obj.side * obj.side;
    end
end

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

t = coder.newtype('mySquare')

3. Change the type of the property side.

t.Properties.side = coder.typeof(int8(3))
t = 

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

Create a type for a string scalar for 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 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. sz 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 class from which to create the coder.ClassType, specified 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

true for dimensions for which sz specifies an upper bound of inf; 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

Enumeration values of 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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

Set complex to true to create a coder.Type object that can represent complex values. The type must support complex data.

Specify local fimath. If fimath is not specified, uses default fimath values.

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

Set sparse to true to create a coder.Type object representing sparse data. The type must support sparse data.

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

Output Arguments

collapse all

New coder.Type object.

Limitations

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

Tips

  • coder.newtype 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.newtype('double',[1 10],1)
    By contrast, the following code specifies a :1-by-:10 double. Both dimensions have a variable size.
    t = coder.newtype('double',[1 10],[1 1])

    Note

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

Alternatives

coder.typeof

Introduced in R2011a