coder.cstructname
Name generated or externally defined structure type in C or C++ code
Syntax
Description
coder.cstructname(
assigns the name var,structName)structName to the C or C++ structure type
generated for the MATLAB® structure or cell array var. Use this syntax
in a function from which you generate code. Place
coder.cstructname after the definition of
var and before the first use of
var. If var is an input argument
to an entry-point function, place coder.cstructname at the
beginning of the function, before any control
flow statements.
coder.cstructname(
assigns the name var,structName,"extern","HeaderFile",headerfile)structName to the C or C++ structure type
generated for the MATLAB variable var and specifies that the external
file headerfile contains the definition of this C or C++
structure type.
You do not have to specify the header file when you use this syntax. However,
it is a best practice to specify the header file so that the code generator
produces the #include statement in the
correct location.
coder.cstructname(
also specifies the run-time memory alignment for the externally defined
structure type var,structName,"extern","HeaderFile",headerfile,"Alignment",alignment)structName. If you have Embedded Coder® and use custom code replacement libraries (CRLs), specify the
alignment so that the code generator can match CRL functions that require
alignment for structures. See Data Alignment for Code Replacement (Embedded Coder). You cannot use this syntax if var is a cell array.
generates a outtype =
coder.cstructname(intype,structName)coder.StructType or coder.CellType object
outname that mimics the structure type of
intype in the generated code. The
TypeName property of the created object is
structName. Use this syntax to create a type object for
code generation. You cannot use this syntax in a function from which you
generate code. You cannot use this syntax in a MATLAB Function
block.
generates a outtype =
coder.cstructname(intype,structName,"extern","HeaderFile",headerfile)coder.StructType or coder.CellType
object outname that mimics the structure type of
intype in the generated code. The
TypeName property of outtype is
structName and the HeaderFile
property of outtype is headerfile. Use
this syntax to create a type object for code generation. When you generate code
and use the outtype type object to specify an input type,
the code generator uses the externally defined structure type for variables with
type outtype. You cannot use this syntax in a MATLAB
Function block.
also specifies the outtype =
coder.cstructname(intype,structName,"extern","HeaderFile",headerfile,"Alignment",alignment)Alignment property of the generated
outtype type object. You can only use this syntax if
intype is a coder.StructType object or
structure. You cannot use this syntax in a MATLAB Function
block.
Examples
Input Arguments
Output Arguments
Limitations
You cannot call
coder.cstructnamedirectly on a global variable. To name the structure type created for a global variable in the generated code, usecoder.cstructnameto create a type object that names the structure type. Then, when you generate code, specify that the global variable has that type. See Name C Structure Type to Use with Global Structure Variable.If you call
coder.cstructnameon a cell array and specify and externally defined type, the fields of the externally defined structure must bef1,f2, …,fn.You cannot call
coder.cstructnamedirectly on a class property.You cannot apply
coder.cstructnameto a homogenous cell array unless the cell array is fixed size.coder.cstructnameforces the cell array to become heterogeneous, and code generation only supports fixed-size heterogeneous cell arrays. See Code Generation for Cell Arrays.
Tips
For information about how the code generator determines the C/C++ types of structure fields, see Mapping MATLAB Types to Types in Generated Code.
Using
coder.cstructnameon a structure array sets the name of the structure type of the base element, not the name of the array. Therefore, you cannot applycoder.cstructnameto a structure array element, and then apply it to the array with a different C structure type name. For example, code generation fails for this code snippet because the second use ofcoder.cstructnamesets the name of the base type tomyStructArrayName, which conflicts with the previously specified name,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");
Applying
coder.cstructnameto an element of a structure array produces the same result as applyingcoder.cstructnameto the entire structure array. If you applycoder.cstructnameto an element of a structure array, you must refer to the element by using a single subscript. For example, you can usevar(1), but notvar(1,1). Applyingcoder.cstructnametovar(:)produces the same result as applyingcoder.cstructnametovarorvar(n).The code generator represents heterogeneous cell arrays as structures in the generated code. When using
coder.cstructnamewith cell arrays:If a cell array is an entry-point function input and its type is permanently homogeneous, you cannot use
coder.cstructnamewith the cell array. For information about when a cell array is permanently homogeneous, see Specify Cell Array Inputs at the Command Line.You cannot use
coder.cstructnamewith a permanently homogeneouscoder.CellTypeobject.The generated
coder.CellTypeobject is permanently heterogeneous.
When you use a structure named by
coder.cstructnamein a project with row-major and column-major array layouts, the code generator renames the structure in certain cases, appendingrow_orcol_to the beginning of the structure name. This renaming provides unique type definitions for the types that are used in both array layouts.MATLAB Function block input and output structures are associated with bus signals. The generated name for the structure type comes from the bus signal name. Do not use
coder.cstructnameto name the structure type for input or output signals. See Create Structures in MATLAB Function Blocks (Simulink).In a MATLAB Function block, the code generator produces structure type names according to identifier naming rules, even if you name the structure type with
coder.cstructname. If you have Embedded Coder, you can customize the naming rules. See Construction of Generated Identifiers (Embedded Coder).
Extended Capabilities
Version History
Introduced in R2011a