主要内容

coder.typeof

创建 coder.Type 对象来表示入口函数输入的类型

说明

注意

您还可以使用代码生成器生成类型编辑器以交互方式创建和编辑 coder.Type 对象。请参阅Create and Edit Input Types by Using the Coder Type Editor

type_obj = coder.typeof(v) 创建一个从 coder.Type 派生的对象,以表示用于代码生成的 v 的类型。使用 coder.typeof 仅指定输入参数类型。当您通过提供示例代码来定义输入类型时,将它与 codegen 函数 -args 选项结合使用,或用在 MATLAB® Coder™ 工程中。不要在您打算从中生成代码的 MATLAB 代码中使用它。

示例

type_obj = coder.typeof(v,sz,variable_dims) 返回 type_obj = coder.typeof(v) 的修改副本,其上界大小由 sz 指定,可变维度由 variable_dims 指定。

示例

type_obj = coder.typeof(v,'Gpu', true) 创建一个从 coder.Type 派生的对象,以将 v 表示为用于代码生成的 GPU 输入类型。此选项需要有效的 GPU Coder™ 许可证。

type_obj = coder.typeof(type_obj) 返回 type_obj 本身。

示例

示例

全部折叠

为简单的固定大小 5x6 双精度矩阵创建一个类型。

coder.typeof(ones(5,6))
ans = 

coder.PrimitiveType
   5×6 double

coder.typeof(0,[5 6])
ans = 

coder.PrimitiveType
   5×6 double

为可变大小双精度矩阵创建一个类型。

coder.typeof(ones(3,3),[],1)
ans = 

coder.PrimitiveType
   :3×:3 double
% ':' indicates 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

为双精度矩阵创建一个类型,该矩阵的第一个维度为无界,第二个维度为固定大小。

coder.typeof(10,[inf,3]) 
ans = 

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

为双精度矩阵创建一个类型,该矩阵的第一个维度为无界,第二个维度为可变大小,上界为 3。

coder.typeof(10,[inf,3],[0 1]) 
ans = 

coder.PrimitiveType
   :inf×:3 double

将固定大小矩阵转换为可变大小矩阵。

coder.typeof(ones(5,5),[],1) 
 ans = 

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

为具有可变大小字段的结构体创建一个类型。

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

创建一个嵌套结构体(一个结构体作为另一个结构体的字段)。

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

创建一个包含可变大小结构体数组作为字段的结构体。

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

为具有可变大小字段的同构元胞数组创建一个类型。

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

为异构元胞数组创建一个类型。

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

从具有相同类但不同大小的元胞数组创建一个可变大小同构元胞数组类型。

1.为包含两个具有不同大小的字符向量元胞数组创建一个类型。元胞数组类型为异构。

coder.typeof({'aa','bbb'})
ans = 

coder.CellType
   1×2 heterogeneous cell 
      f1: 1×2 char
      f2: 1×3 char

2.使用相同的元胞数组输入创建一个类型。这次指定元胞数组类型具有可变大小维度。元胞数组类型为同构。

coder.typeof({'aa','bbb'},[1,10],[0,1])
ans = 

coder.CellType
   1×:10 locked homogeneous cell 
      base: 1×:3 char
% ':' indicates variable-size dimensions

将固定大小数组更改为有界可变大小数组。

为值类对象创建一个类型。

1.创建以下值类:

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.创建一个 mySquare 对象。

sq_obj = coder.typeof(mySquare(4))
sq_obj = 

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

3.为与 sq_obj 具有相同属性的对象创建一个类型。

t = coder.typeof(sq_obj)
t = 

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

您也可以从类定义创建类型:

t = coder.typeof(mySquare(4))
t = 

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

定义一个字符串标量。例如:

s = "mystring";

s 创建一个类型。

t = coder.typeof(s);

将该类型对象的 StringLength 属性指定为字符串长度的上界,并将 VariableStringLength 设置为 true。指定类型对象 t 的大小可变,上界为 10。

t.StringLength = 10;
t.VariableStringLength = true;

要指定 t 的大小可变且没有上界,请使用以下代码:

t.StringLength = Inf;
这会自动将 VariableStringLength 属性设置为 true

通过使用 -args 选项将类型传递给 codegen

codegen myFunction -args {t}

输入参数

全部折叠

v 可以是 MATLAB 数值、逻辑值、字符、枚举或定点数组。v 也可以是包含上述类型的元胞数组、结构体或值类。

v 是元胞数组且其元素具有相同的类但大小不同时,如果您指定可变大小维度,coder.typeof 将创建一个同构元胞数组类型。如果元素具有不同的类,coder.typeof 会报告错误。

示例: coder.typeof(ones(5,6));

数据类型: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
复数支持:

指定类型对象的每个维度的大小向量。

如果 sz 对某一维度指定 inf,则该维度的大小为无界,并且该维度为可变大小。当 sz[] 时,v 的上界不变。

如果未指定大小,sz 将采用 v 的默认维度。

示例: coder.typeof(0,[5,6]);

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

指定每个维度是可变大小 (true) 还是固定大小 (false) 的逻辑向量。对于元胞数组,如果元素具有不同的类,则您无法指定可变大小维度。

如果不指定 variable_dims 输入参数,则该类型的有界维度为固定大小。

标量 variable_dims 适用于所有维度。但是,如果 variable_dims1,则单一维度的大小保持固定。

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

数据类型: logical

coder.Type 对象,表示用于代码生成的 v 的类型。

示例: type_obj = coder.typeof(ones(5,6));

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
复数支持:

输出参量

全部折叠

coder.Type 对象,表示用于代码生成的 v 的类型。

示例: type_obj = coder.typeof(ones(5,6));

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
复数支持:

限制

  • 对于稀疏矩阵,coder.typeof 会删除可变大小维度的上界。

  • 不支持标量 GPU 数组、结构体、元胞数组、类、枚举类型、字符、半精度和定点数据类型。

  • 当使用 coder.typeof 表示 GPU 数组时,GPU 代码配置对象的内存分配 (malloc) 模式属性必须设置为 'discrete'

  • 您无法将未配置的字典传递给 coder.typeof

提示

  • coder.typeof 会固定单一维度的大小,除非 variable_dims 参量显式指定单一维度的大小可变。

    例如,以下代码指定一个 1×:10 的双精度数组。第一个维度(单一维度)具有固定大小。第二个维度具有可变大小。

    t = coder.typeof(5,[1 10],1)
    而以下代码指定一个 :1×:10 双精度数组。两个维度都具有可变大小。
    t = coder.typeof(5,[1 10],[1 1])

    注意

    对于 MATLAB Function 模块,输入或输出信号的单一维度不能有可变大小。

  • 如果您已使用类型函数指定输入变量的类型,请不要使用 coder.typeof,除非您还想指定大小。例如,使用语法 single(0),而不是 coder.typeof(single(0))

  • 对于元胞数组类型,coder.typeof 确定元胞数组类型是同构还是异构。

    如果元胞数组元素具有相同的类和大小,coder.typeof 将返回同构元胞数组类型。

    如果元素具有不同的类,coder.typeof 将返回异构元胞数组类型。

    对于一些元胞数组,同构或异构的分类具有多义性。例如,{1 [2 3]} 的类型可以是 1×2 异构类型,其中第一个元素是双精度值,第二个元素是 1×2 双精度数组。该类型也可以是 1×3 同构类型,其中元素的类为双精度,大小为 1×:2。对于这些具有多义性的情况,coder.typeof 使用启发式方法将类型分类为同构或异构。如果您需要不同分类,请使用 coder.CellType makeHomogeneousmakeHeterogeneous 方法来创建具有所需分类的类型。makeHomogeneous 方法创建类型的同构副本。makeHeterogeneous 方法创建类型的异构副本。

    makeHomogeneousmakeHeterogeneous 方法永久性地将分类指定为同构和异构。您不能在后面使用这些方法之一来创建具有不同分类的副本。

  • 您可以使用 coder.typeof 创建一个无界 GPU 数组输入,并将其传递给入口函数。例如,使用 coder.typeof(int32(1),[inf,1],'Gpu',true) 创建一个 GPU 输入。

  • 在使用 GPU 数组类型生成代码的过程中,如果入口函数的一个输入是 GPU 数组类型,则输出变量均为 GPU 数组类型,前提是它们支持 GPU 代码生成。例如,如果入口函数返回 struct,并且由于不支持 struct,则生成的代码会返回 CPU 输出。但是,如果返回了支持的矩阵类型,则生成的代码将返回 GPU 输出。

版本历史记录

在 R2011a 中推出

全部展开