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

GoalSpecify
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 Naming Rules to Simulink Data Objects

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.

Specify Naming Rule Using a Function

This example shows how to 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, 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];
  2. Open the model rtwdemo_namerules.

  3. Double-click the yellow box labeled View Symbols Configuration to open the Code Generation > Identifiers pane in the Configuration Parameters dialog box.

  4. From the Parameter naming drop-down list, select Custom M-function.

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

  6. Click Apply.

  7. Generate code for the model.

  8. 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.

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. 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.

Override Data Object Naming Rules

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.

  1. Open the model rtwdemo_namerules.

  2. On the Modeling tab, click Model Data Editor.

  3. In the Model Data Editor, on the Parameters tab, click the Show/refresh additional information button.

  4. In the data table, find the row that corresponds to the Simulink.Parameter object G1, which resides in the base workspace.

  5. In the row, double-click the parameter icon .

  6. In the Model Explorer Dialog pane (the right pane), for G1, specify the Identifier property as mySpecialParam. Click Apply.

  7. Generate code for the model.

  8. 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.

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.

model='rtwdemo_namerules';
open_system(model)

% Cleanup
rtwdemoclean;
close_system(model,0)

See Also


[1] ANSI is a registered trademark of the American National Standards Institute, Inc.