For GRT and RSim targets, the code generator constructs identifiers for variables and functions in the generated code. For ERT targets, you can customize the naming of identifiers in the generated code by specifying parameters on the Code Generation > Identifiers pane in the Configuration Parameters dialog box. You can also specify parameters that control identifiers generated from Simulink® data objects. For detailed information about these parameters, see Model Configuration Parameters: Code Generation Identifiers.
Goal | Specify |
---|---|
Set the maximum number of characters that the code generator uses for function,
typedef , and variable names (default 31). | An integer value for the Maximum identifier length parameter. For more information, see Specify Identifier Length to Avoid Naming Collisions. If you expect your model to generate lengthy identifiers (due to use of long signal or parameter names, for example), or if identifiers are mangled more than you expect, increase the value of this parameter. |
Define a macro that specifies certain text included within generated identifiers for:
| A macro for the Identifier format control parameters. For more information, see Identifier Format Control. See also Exceptions to Identifier Formatting Conventions and Identifier Format Control Parameters Limitations. |
Set the minimum number of characters that the code generator uses for the mangling text. | An integer value for the Minimum mangle length parameter. For more information, see Control Name Mangling in Generated Identifiers |
Control whether the software uses shortened names for system-generated identifiers. |
|
Control whether the generated code expresses scalar inlined parameter values as literal values or as macros. | The value
|
When your model uses Simulink data objects from the Simulink
package, identifiers in
generated code copy the names of the objects by default. For example, a
Simulink.Signal
object named Speed
appears as the
identifier Speed
in generated code.
You can control these identifiers by specifying naming rules that are specific to Simulink data objects. On the Code Generation > Identifiers pane of the Configuration Parameters dialog box, adjust the settings in the Simulink data object naming rules section.
When you specify naming rules for generated code, follow ANSI® C[1] /C++ rules for naming identifiers.
This example shows how to customize identifiers in generated code by defining a MATLAB® function.
Write a MATLAB function that returns an identifier by modifying a data object name, and
save the function in your working folder. For example, the following function returns
an identifier name by appending the text _param
to a data object
name.
function revisedName = append_text(name, object) % APPEND_TEXT: Returns an identifier for generated % code by appending text to a data object name. % % Input arguments: % name: data object name as spelled in model % object: target data object % % Output arguments: % revisedName: altered identifier returned for use in % generated code. % % text = '_param'; revisedName = [name,text];
Open the model rtwdemo_namerules
.
Double-click the yellow box labeled View Symbols Configuration to open the Code Generation > Identifiers pane in the Configuration Parameters dialog box.
From the Parameter naming drop-down list, select Custom M-function.
In the M-function field, type the name of the file that
defines the MATLAB function, append_text.m
.
Click Apply.
Generate code for the model.
Inspect the code generation report to confirm the parameter object naming rule.
For example, the generated file rtwdemo_namerules.h
represents the
parameter objects G1
, G2
, and
G3
with the variables G1_param
,
G2_param
, and G3_param
.
Define
You can specify a naming rule that applies only to Simulink data objects whose storage class you set to Define
. For
these data objects, the specified naming rule overrides the other parameter and signal
object naming rules. On the Code Generation > Identifiers pane in the Configuration Parameters dialog box, adjust the #define naming
setting.
This example shows how to override a data object naming rule for a single data object.
You can override data object naming rules by specifying the
Identifier
property of an individual Simulink data object. Generated code uses the text that you specify as the identifier
to represent the data object, regardless of naming rules.
Open the model rtwdemo_namerules
.
On the Modeling tab, click Model Data Editor.
In the Model Data Editor, on the Parameters tab, click the Show/refresh additional information button.
In the data table, find the row that corresponds to the
Simulink.Parameter
object G1
, which resides in
the base workspace.
In the row, double-click the parameter icon .
In the Model Explorer Dialog pane (the right pane), for G1
,
specify the Identifier
property as
mySpecialParam
. Click Apply.
Generate code for the model.
In the code generation report, confirm the identifier for the parameter object
G1
. The generated file rtwdemo_namerules.h
represents G1
with the variable
mySpecialParam
.
This example shows how to apply uniform naming rules for Simulink® data objects, including signals, parameters, and data store memory variables.
model='rtwdemo_namerules';
open_system(model)
% Cleanup
rtwdemoclean;
close_system(model,0)
[1] ANSI is a registered trademark of the American National Standards Institute, Inc.