Name C structure type in generated code
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).
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 in a MATLAB Function block.
You cannot apply coder.cstructname
directly to a global
variable. To name the structure type to use with a global variable, use
coder.cstructname
to create a type object that names
the structure type. Then, when you run codegen
, specify
that the global variable has that type. See Name the C Structure Type to Use With a Global Structure Variable.
For cell array inputs, the field names of externally defined structures must
be f1
, f2
, and so on.
You cannot apply coder.cstructname
directly to a class
property.
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.cstructname
on a structure array sets the
name of the structure type of the base element, not the name of the array.
Therefore, you cannot apply coder.cstructname
to 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 second
coder.cstructname
attempts to set the name of the base
type to myStructArrayName
, 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.cstructname
to an element of a structure
array produces the same result as applying
coder.cstructname
to the entire structure array. If you
apply coder.cstructname
to an element of a structure array,
you must refer to the element by using a single subscript. For example, you can
use var(1)
, but not var(1,1)
. Applying
coder.cstructname
to var(:)
produces
the same result as applying coder.cstructname
to
var
or var(n)
.
Heterogeneous cell arrays are represented as structures in the generated code.
Here are considerations for using coder.cstructname
with
cell arrays:
In a function from which you generate code, using
coder.cstructname
with 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 use
coder.cstructname
with the cell
array.
Using coder.cstructname
with a homogeneous
coder.CellType
object
intype
makes the returned object
heterogeneous. Therefore, you cannot use
coder.cstructname
with a permanently
homogeneous coder.CellType
object.
For information about when a cell array is permanently homogeneous,
see Specify Cell Array Inputs at the Command Line.
When used with a coder.CellType
object, coder.cstructname
creates a coder.CellType
object that is
permanently heterogeneous.
When you use a structure named by coder.cstructname
in a
project with row-major and column-major array layouts, the code generator
renames the structure in certain cases, appending row_
or
col_
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.cstructname
to name the structure
type for input or output signals. See Create Structures in MATLAB Function Blocks (Simulink).
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).