coder.cstructname
在生成代码中命名 C 结构体类型
语法
说明
coder.cstructname
为生成的或外部定义的、用于 MATLAB® 变量(在生成的代码中表示为结构体)的 C 语言结构体类型命名。
coder.cstructname(
为针对 MATLAB 变量 var
,structName
)var
生成的 C 语言结构体类型命名。输入 var
可以是结构体或元胞数组。在为函数生成代码时,可在该函数中使用此语法。将 coder.cstructname
放置在定义 var
的语句之后,第一次使用 var
的语句之前。如果 var
是入口(顶层)函数输入参数,请将 coder.cstructname
放在函数的开头且在任何控制流语句之前。
coder.cstructname(
指定用作 var
,structName
,'extern','HeaderFile',headerfile
)var
的 C 结构体类型的名称为 structName
,且是在外部文件 headerfileName
中定义的。
可以在不指定头文件的情况下使用 'extern'
选项。但是,最好指定头文件,以便代码生成器在正确的位置生成 #include
语句。
coder.cstructname(
还指定外部定义的结构体类型 var
,structName
,'extern','HeaderFile',headerfile
,'Alignment',alignment
)structName
的运行时内存对齐方式。如果您有 Embedded Coder® 并使用自定义代码替换库 (CRL),请指定对齐方式,以便代码生成器可以匹配要求结构体对齐的 CRL 函数。请参阅Data Alignment for Code Replacement (Embedded Coder)。
返回一个结构体或一个元胞数组类型对象 outtype
= coder.cstructname(intype
,structName
)outtype
,它指定要生成的 C 结构体类型的名称。coder.cstructname
使用输入类型 intype
的属性创建 outtype
。然后,它将 TypeName
属性设置为 structName
。使用此语法创建可用于 codegen
-args
选项的类型对象。在为函数生成代码时,不能在该函数中使用此语法。
您不能在 MATLAB Function 模块中使用此语法。
返回指定外部定义的 C 结构体类型的名称和位置的类型对象 outtype
= coder.cstructname(intype
,structName
,'extern','HeaderFile',headerfile
)outtype
。代码生成器对类型为 outtype
的变量使用外部定义的结构体类型。
您不能在 MATLAB Function 模块中使用此语法。
创建一个还指定 C 结构体类型对齐方式的类型对象 outtype
= coder.cstructname(intype
,structName
,'extern','HeaderFile',headerfile
,'Alignment',alignment
)outtype
。
您不能在 MATLAB Function 模块中使用此语法。
示例
输入参数
限制
您不能将
coder.cstructname
直接应用于全局变量。要命名用于全局变量的结构体类型,请使用coder.cstructname
创建命名结构体类型的类型对象。然后,当您运行codegen
时,指定全局变量具有该类型。请参阅Name the C Structure Type to Use With a Global Structure Variable。对于元胞数组输入,外部定义的结构体的字段名称必须为
f1
、f2
等,以此类推。您无法将
coder.cstructname
直接应用于类属性。
提示
有关代码生成器如何确定结构体字段的 C/C++ 类型的信息,请参阅Mapping MATLAB Types to Types in Generated Code。
对结构体数组使用
coder.cstructname
将设置基元素的结构体类型的名称,而不是数组的名称。因此,您不能将coder.cstructname
应用于结构体数组元素,然后将其应用于具有不同 C 结构体类型名称的数组。例如,不允许使用以下代码。第二个coder.cstructname
尝试将基类型的名称设置为myStructArrayName
,这与以前指定的名称myStructName
发生冲突。% Define scalar structure with field a myStruct = struct('a', 0); coder.cstructname(myStruct,'myStructName'); % Define array of structure with field a myStructArray = repmat(myStruct,4,6); coder.cstructname(myStructArray,'myStructArrayName');
将
coder.cstructname
应用于结构体数组的元素会产生与将coder.cstructname
应用于整个结构体数组相同的结果。如果将coder.cstructname
应用于结构体数组的元素,则必须使用单一下标引用该元素。例如,您可以使用var(1)
,但不能使用var(1,1)
。将coder.cstructname
应用于var(:)
会产生与将coder.cstructname
应用于var
或var(n)
相同的结果。异构元胞数组在生成的代码中表示为结构体。以下是对元胞数组使用
coder.cstructname
的注意事项:在要生成代码的函数中,将
coder.cstructname
与元胞数组变量结合使用会生成异构元胞数组。因此,如果元胞数组是入口函数输入,并且其类型为永久性同构,则不能将coder.cstructname
与元胞数组结合使用。将
coder.cstructname
与同构coder.CellType
对象intype
结合使用会使返回的对象为异构类型。因此,不能将coder.cstructname
与永久性同构coder.CellType
对象结合使用。有关元胞数组何时为永久性同构的信息,请参阅Specify Cell Array Inputs at the Command Line。当与
coder.CellType
对象结合使用时,coder.cstructname
会创建永久性异构的coder.CellType
对象。
在具有行优先和列优先数组布局的工程中使用由
coder.cstructname
命名的结构体时,代码生成器会在某些情况下重命名结构体,将row_
或col_
附加到结构体名称的开头。此重命名为这两个数组布局中使用的类型提供唯一的类型定义。下列提示仅适用于 MATLAB Function 模块:
MATLAB Function 模块输入和输出结构体与总线信号相关联。生成的结构体类型名称来自总线信号名称。请勿使用
coder.cstructname
为输入或输出信号的结构体类型命名。请参阅在 MATLAB Function 模块中创建结构体 (Simulink)。代码生成器会根据标识符命名规则生成结构体类型名称,即使您使用
coder.cstructname
命名结构体类型也是如此。如果您有 Embedded Coder,则可以自定义命名规则。请参阅Construction of Generated Identifiers (Embedded Coder)。
扩展功能
版本历史记录
在 R2011a 中推出