Main Content

Customize Generated Identifier Naming Rules

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.

Apply Naming Rules to Identifiers Globally

GoalSpecification
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:

  • Global variables

  • Global types

  • Field names of global types

  • Subsystem methods

  • Subsystem method arguments

  • Local temporary variables

  • Local block output variables

  • Constant macros

  • Shared utilities identifier format

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.

Shortened for the System-generated identifiers parameter. This setting:

  • Provides more space for user names.

  • Provides a more predictable and consistent naming system that uses camel case.

  • Does not include underscores or plurals.

  • Provides consistent abbreviations for both a type and a variable.

Control whether the generated code expresses scalar inlined parameter values as literal values or as macros.

The value Literals or Macros for the Generate scalar inlined parameters as parameter.

  • Literals: If you set Default parameter behavior to Inlined, parameters are expressed as numeric constants.

  • Macros: Parameters are expressed as variables (with #define macros). This setting makes code more readable.

Apply Custom Naming Conventions to Identifiers

This example shows how to apply uniform naming rules for Simulink® data objects, including signals, parameters, and data store memory variables.

When your model uses Simulink data objects from the Simulink package, identifiers in the 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. When you specify naming rules for generated code, follow ANSI® C/C++ rules for naming identifiers.

Apply Lowercase Naming Rule

1. Open the model.

model='rtwdemo_namerules';
open_system(model)

ApplyCustomNamingConventionsToIdentifiersExample.png

2. Open the C Code tab and click View Code.

3. In the Model Configuration Parameters, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. In this model, all three rules are set to Force lower case.

RulesA.png

4. Generate code for the model. The generated file rtwdemo_namerules.c uses lowercase identifiers for parameters.

/* Exported block parameters */
real_T f1 = 2.0;                       /* Variable: F1
                                        * Referenced by: '<Root>/Chart'
                                        */
real_T g1 = 3.0;                       /* Variable: G1
                                        * Referenced by:
                                        *   '<Root>/Chart'
                                        *   '<S3>/Gain1'
                                        */
real_T g2 = 4.0;                       /* Variable: G2
                                        * Referenced by: '<S3>/Gain2'
                                        */
real_T g3 = 5.0;                       /* Variable: G3
                                        * Referenced by: '<S2>/Gain'
                                        */

Apply Naming Rule Using a Function

You can customize identifiers in generated code by defining a MATLAB® function.

1. Write a MATLAB function that returns an identifier by modifying a data object name, then save the function in your working folder. For example, the following function returns an identifier name by appending a data object's data type to that data object's 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: information about model
%
% Output arguments:
% revisedName: altered identifier returned for use in
% generated code.
%
%
DataType = Simulink.data.evalinGlobal(object.modelName,[name,'.DataType']);

revisedName = [name,'_',DataType];

2. In the Model Configuration Parameters, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. From the Parameter naming drop-down list, select Custom M-function.

3. In the M-function field under Parameter naming, type the name of the file that defines the MATLAB function, append_text.m, then click Apply.

RulesB.png

4. Generate code for the model. The generated code implements the parameter object naming rule in rtwdemo_namerules.c.

/* Exported block parameters */
real_T F1_auto = 2.0;                  /* Variable: F1
                                        * Referenced by: '<Root>/Chart'
                                        */
real_T G1_auto = 3.0;                  /* Variable: G1
                                        * Referenced by:
                                        *   '<Root>/Chart'
                                        *   '<S3>/Gain1'
                                        */
real_T G2_auto = 4.0;                  /* Variable: G2
                                        * Referenced by: '<S3>/Gain2'
                                        */
real_T G3_auto = 5.0;                  /* Variable: G3
                                        * Referenced by: '<S2>/Gain'
                                        */

Specify Naming Rule for Storage Class define

You can specify a naming rule that applies only to Simulink data objects whose storage class you set to Define.

1. Open the Code Mappings Editor. On the C Code tab, select Code Interface > Default Code Mappings.

2. In the Parameters tab of the Code Mappings Editor, find the data table row labeled External Parameter Objects. Click the Refresh button in that row.

3. Find the row in the data table that corresponds to the Simulink.Parameter object G1, which resides in the base workspace. From the drop-down menu, set the Storage Class for G1 to Define.

3.png

4. In the Model Configuration Properties, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. From the #define naming drop-down list, select Custom M-function.

5. In the M-function field under #define naming, type the name of the file that defines the MATLAB function, append_text.m.

6. From the Parameter naming drop-down list, select Force lower case, then click Apply.

RulesC.png

7. Generate code for the model. The generated file rtwdemo_namerules.h represents G1 with G1_auto and K1 with K1_auto.

/* Definition for custom storage class: Define */
#define G1_auto                        3.0                       /* Referenced by:
                                                                  * '<Root>/Chart'
                                                                  * '<S3>/Gain1'
                                                                  */
#define K1_auto                        6.0                       /* Referenced by: '<Root>/Chart' */

Override Data Object Naming Rules

You can override data object naming rules for individual data objects by specifying the Identifier property. Generated code uses the text that you specify as the identifier to represent the data object, regardless of naming rules.

1. In the Parameters tab of the Code Mappings Editor, find the row in the data table that corresponds to the Simulink.Parameter object G2. Right click the row and select Open.

2. In the Property Inspector, go to the Code Generation tab. Specify the Identifier property as myIdentifier. Click Apply.

3. Generate code for the model. The generated file rtwdemo_namerules.c represents G2 with the variable myIdentifier.

/* Exported block parameters */
real_T f1 = 2.0;                       /* Variable: F1
                                        * Referenced by: '<Root>/Chart'
                                        */
real_T myIdentifier = 4.0;             /* Variable: G2
                                        * Referenced by: '<S3>/Gain2'
                                        */
real_T g3 = 5.0;                       /* Variable: G3
                                        * Referenced by: '<S2>/Gain'
                                        */

See Also