main content

create coder.type object to represent the type of an entry-凯发k8网页登录

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

description

example

note

you can also create and edit coder.type objects interactively by using the coder type editor. see .

type_obj = coder.typeof(v) creates an object that is derived from coder.type to represent the type of v for code generation. use coder.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 = coder.typeof(v,sz,variable_dims) returns a modified copy of type_obj = coder.typeof(v) with upper bound size specified by sz and variable dimensions specified by variable_dims.

type_obj = coder.typeof(v,'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.

example

type_obj = coder.typeof(type_obj) returns type_obj itself.

examples

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

coder.typeof(ones(5,6))
ans = 
coder.primitivetype
   5×6 double
coder.typeof(0,[5 6])
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
coder.typeof(10,[1 5],1) 
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.

coder.typeof(10,[inf,3]) 
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 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 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

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

define a string scalar. for example:

s = "mystring";

create a type from s.

t = coder.typeof(s);

assign the stringlength property of the type object the upper bound of the string length and set variablestringlength 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:

t.stringlength = inf;
this automatically sets the variablestringlength property to true.

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

codegen myfunction -args {t}

input arguments

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

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

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

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

data types: logical

coder.type object to represent the type of v 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

coder.type object to represent the type of v 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.

  • for representing gpu arrays, 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.typeof to represent gpu arrays, the memory allocation (malloc) mode property of the gpu code configuration object must be set to be 'discrete'.

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 of coder.typeof(single(0)), use the syntax single(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.celltype makehomogeneous or makeheterogeneous methods to make a type with the classification that you want. the makehomogeneous method makes a homogeneous copy of a type. the makeheterogeneous method makes a heterogeneous copy of a type.

    the makehomogeneous and makeheterogeneous 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.

  • 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 because struct 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

see also

| | | | | | | | | | |

topics

    网站地图