Documentation

coder.typeof

Package: coder

Convert MATLAB value into its canonical type

Syntax

t = coder.typeof(v)
t = coder.typeof(v, sz, variable_dims)
t = coder.typeof(t)

Description

t = coder.typeof(v) creates a coder.Type object denoting the smallest nonconstant type that contains v. v must be a MATLAB® numeric, logical, char, enumeration or fixed-point array, or a cell array or struct constructed from these types. Use coder.typeof to specify only input parameter types. For example, use it with the fiaccel function -args option. Do not use it in MATLAB code from which you intend to generate a MEX function.

t = coder.typeof(v, sz, variable_dims) returns a modified copy of t = coder.typeof(v) with (upper bound) size specified by 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 sz is [], the (upper bound) sizes of v do not change. If you do not specify the variable_dims input parameter, the bounded dimensions of the type are fixed. When variable_dims is a scalar, it is applied to bounded dimensions or dimensions that are 1 or 0, which are fixed.

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.

t = coder.typeof(t), where t is a coder.Type object, returns t itself.

Input Arguments

sz

Size vector specifying each dimension of type object.

t

coder.Type object

v

MATLAB expression that describes the set of values represented by this type.

v must be a MATLAB numeric, logical, char, enumeration or fixed-point array, or a cell array or struct constructed from the preceding types.

variable_dims

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.

Output Arguments

t

coder.Type object

Examples

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

coder.typeof(ones(5, 6))         
 % returns 5x6 double
coder.typeof(0, [5 6])           
 % also returns 5x6 double

Create a type for a variable-size matrix of doubles.

coder.typeof(ones(3,3), [], 1)    
% returns :3 x :3 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)
% Returns 
% coder.StructType
%    1x1 struct
%      a:  :3x:5 double
%      b:  3x3  double
% ':' 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})
% Returns 
% coder.CellType
%   1x2 homogeneous cell 
%      base: :3x: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})
% Returns 
% coder.CellType
%   1x2 heterogeneous cell 
%      f0: 1x1 char
%      f1: 1x1 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 has two strings with different sizes. The cell array type is heterogeneous.

    coder.typeof({'aa', 'bbb'})
    % Returns
    % coder.CellType
    %   1x2 heterogeneous cell 
    %      f0: 1x2 char
    %      f1: 1x3 char
    
  2. Create a type 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])
    % Returns
    % coder.CellType
    %   1x:10 homogeneous cell 
    %      base: 1x:3 char
    

Create a type for a matrix with fixed-size and variable-size dimensions.

coder.typeof(0, [2,3,4], [1 0 1]);
% Returns :2x3x:4 double 
% ':' indicates variable-size dimensions
coder.typeof(10, [1 5], 1) 
% returns double 1 x  :5
% ':' 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]) 
% returns double:inf x 3
% ':' indicates variable-size dimensions

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

coder.typeof(10, [inf,3],[0 1]) 
% returns double :inf x :3
% ':' indicates variable-size dimensions

Convert a fixed-size matrix to a variable-size matrix.

 coder.typeof(ones(5,5), [], 1) 
% returns double :5x:5
% ':' 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)  
% Returns 
% coder.StructType
% SuperS:  1x1 struct
%   with fields 
%      x:  1x1 struct
%         with fields
%             a: 1x1 double
%             b: 1x1 single
%      y:  1x1  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)  
% Returns 
% coder.StructType
% SuperS:  1x1 struct
%   with fields 
%      x:  1x:inf struct
%         with fields
%             a: 1x1 double
%             b: 1x1 single
%      y:  1x1  single
% ':' indicates variable-size dimensions

Tips

  • If you are already specifying the type of an input variable 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, the 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, respectively. You cannot later use one of these methods to create a copy that has a different classification.

Introduced in R2011a

Was this topic helpful?