coder.cstructname
Name C structure type in generated code
Syntax
Description
coder.cstructname names the generated or externally
defined C structure type to use for MATLAB® variables that are represented as structures in generated
code.
coder.cstructname(
names the C structure type generated for the MATLAB variable var,structName)var. The input
var can be a structure or a cell array. 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 entry-point
(top-level) function input argument, place
coder.cstructname at the beginning of the function,
before any control flow statements.
coder.cstructname(
specifies that the C structure type to use for var,structName,'extern','HeaderFile',headerfile)var has the
name structName and is defined in the external file,
headerfileName.
It is possible to use the 'extern' option without
specifying the header file. 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.
returns a structure or cell array type object outtype =
coder.cstructname(intype,structName)outtype that
specifies the name of the C structure type to generate.
coder.cstructname creates outtype
with the properties of the input type intype. Then, it sets
the TypeName property to structName. Use
this syntax to create a type object that you use with the
codegen
-args option. You cannot use this syntax in a function from
which you generate code. You cannot use this syntax in a MATLAB
Function block.
returns a type object outtype =
coder.cstructname(intype,structName,'extern','HeaderFile',headerfile)outtype that specifies the name and
location of an externally defined C structure 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.
creates a type object outtype =
coder.cstructname(intype,structName,'extern','HeaderFile',headerfile,'Alignment',alignment)outtype that also specifies the C
structure type alignment. You cannot use this syntax if
intype is a coder.CellType object or a
cell array. You cannot use this syntax in a MATLAB Function
block.
Examples
Input Arguments
Limitations
You cannot apply
coder.cstructnamedirectly to a global variable. To name the structure type to use with a global variable, usecoder.cstructnameto create a type object that names the structure type. Then, when you runcodegen, specify that the global variable has that type. See Name C Structure Type to Use with Global Structure Variable (MATLAB Coder).For cell array inputs, the field names of externally defined structures must be
f1,f2, and so on.You cannot apply
coder.cstructnamedirectly to a class property.
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 (MATLAB Coder).
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, the following code is not allowed. The secondcoder.cstructnameattempts to set 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).Heterogeneous cell arrays are represented as structures in the generated code. Here are considerations for using
coder.cstructnamewith cell arrays:In a function from which you generate code, using
coder.cstructnamewith a cell array variable makes the cell array heterogeneous. Therefore, if a cell array is an entry-point function input and its type is permanently homogeneous, then you cannot usecoder.cstructnamewith the cell array.Using
coder.cstructnamewith a homogeneouscoder.CellTypeobjectintypemakes the returned object heterogeneous. Therefore, you cannot usecoder.cstructnamewith a permanently homogeneouscoder.CellTypeobject. For information about when a cell array is permanently homogeneous, see Specify Cell Array Inputs at the Command Line (MATLAB Coder).When used with a
coder.CellTypeobject,coder.cstructnamecreates acoder.CellTypeobject that 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.These tips apply only to MATLAB Function blocks:
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.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