Main Content

Control Formatting of Identifiers

This example shows how to customize generated identifiers by specifying the Identifier format control parameters on the Code Generation > Identifiers pane in the Configuration Parameters dialog box. To maintain model traceability, incremental revisions to a model must have minimal impact on the identifier names that appear in the generated code. This example shows how to minimally impact the identifier names by specifying the Identifier format control parameters.

Inspect Model

Open the IdentifierFormatting model.

model = 'IdentifierFormatting';
open_system(model)

The model contains two subsystems, multiply_by_2 and multiply_by_4, and a Stateflow® Chart, determine_if_positive. Each subsystem contains a Gain block.

Inspect Model Configuration Parameters

Open the model configuration parameters dialog by selecting Model Settings on the Modeling tab. On the left pane, select Code Generation > Identifiers.

Inspect the Subsystem methods parameter. By default, this parameter has the value $R$N$M$F. The $R token expands to the model name, IdentifierFormatting. The $N token expands to the name of the Simulink object that corresponds to the identifier, such as multiply_by_4. If you generate separate output and update methods for subsystems, the $F token expands to _Update or _Output for each generated method.

After expanding these tokens, if the code generator generates multiple identifiers with the same name, it expands the $M token to name-mangling text to prevent naming collisions.

Generate Subsystem Method Identifiers Without $N

Remove the $N token from the Subsystem methods parameter.

Generate the code. The generated code includes these functions for the subsystems in the model:

static void IdentifierFormatting_c(void);
static void IdentifierFormatting_a(void);

Because the model is not configured to generate separate output and update subsystem methods, the $F token does not expand. The $R token expands to the root model name IdentifierFormatting. Both the multiply_by_2 and multiply_by_4 subsystems generate the IdentifierFormatting function name, which causes an identifier naming collision. To resolve the collision, the code generator expands the $M token to a single character of name-mangling text in each identifier name: c in the first function and a in the second function.

Generate Subsystem Method Identifiers with $N

Restore the Subsystem methods configuration parameter to the default value of $R$N$M$F.

Generate the code. The generated code includes these functions for the subsystems in the model:

static void IdentifierFormatting_multiply_by_2(void);
static void IdentifierFormatting_multiply_by_4(void);

Because the $N token expands to a unique value for each subsystem, the code generator does not generate name-mangling text.

Related Topics