Identifier Format Control
You can customize generated identifiers by specifying the Identifier format
control parameters on the Code Generation > Identifiers
pane in the Configuration Parameters dialog box. For each parameter, you can enter a macro
that specifies whether, and in what order, certain text is included within generated
identifiers. For example, you can specify that the root model name be inserted into each
identifier using the
The macro can include:
Valid tokens, which are listed in Identifier Format Tokens. You can use or omit tokens depending on what you want to include in the identifier name. The Shared utilities identifier format parameter requires you to specify the checksum token,
$C. The other parameters require the mangling token,
$M. For more information, see Avoid Identifier Naming Collisions. The mangling token is subject to the use and ordering restrictions noted in Identifier Format Control Parameter Values.
Token decorators, which are listed in Control Case by Using Token Decorators. You can use token decorators to control the case of generated identifiers for each token.
Valid C or C++ language identifier characters (
a-z, A-Z, _ , 0-9).
The build process generates each identifier by expanding tokens and inserting the
resultant text into the identifier. The tokens are expanded in the order listed in Identifier Format Tokens. Groups of characters are inserted in the positions that
you specify around tokens directly into the identifier. Contiguous token expansions are
separated by the underscore (
Identifier Format Tokens
This token is required for Shared utilities identifier
format. If the identifier exceeds the Maximum identifier
length, the code generator inserts an 8-character checksum to avoid
naming collisions. For shared utilities that you generate from a MATLAB Function block, the code generator replaces the
This token is required. If necessary, the
code generator inserts name-mangling text to avoid naming collisions. Modify
checksum character length by using Shared checksum length
parameter. The position of the
Insert text that you specify for the
Insert method name (for example,
Insert name of object (block, signal or signal object, state, parameter,
shared utility function or parameter object) for which the identifier is being
generated. For a list of resolutions for the
Insert root model name into identifier, replacing unsupported characters
with the underscore (
Note: This token replaces the Prefix model name to global identifiers option in previous releases.
Insert tag indicating system hierarchy level. For root-level blocks, the
tag is the text
Note: This token replaces the Include System Hierarchy Number in Identifiers option in previous releases.
Insert data type acronym (for example,
Note: This token replaces the Include data type acronym in identifier option in previous releases.
For data items, insert the name of a storage class that is associated with the data item. This token is also available in the naming rule that you specify for the Header File for a storage class in the Embedded Coder® Dictionary.
For service interfaces, insert the name of the service.
Insert the file type.
This token applies only to sender, receiver, data transfer, and timer service function naming rules. Insert the name of the entry-point callable function that encloses the service function call.
Identifier Format Control Parameter Values lists the default macro value, the supported tokens, and the applicable restrictions for each Identifier format control parameter.
Identifier Format Control Parameter Values
|Parameter||Default Value||Supported Tokens||Restrictions|
|Field name of global types|
|Subsystem method arguments|
|Local temporary variables|
|Local block output variables|
|Shared utilities identifier format|
|EMX array utility functions identifier
|EMX array types identifier
Non-ERT-based targets (such as the GRT target) implicitly use a default
$R$N$M specification. This default specification consists of the root
model name, followed by the name of the generating object (signal, parameter, state, and so
on), followed by name-mangling text.
$N Embedded Coder Dictionary Resolution
$N token resolves to the name of the object for which an
identifier is being generated. The object type varies by the context of the naming rule.
This table lists the objects to which
$N resolves for items in the
Embedded Coder Dictionary.
|Category||Service Interface||Customization Entity||Default Naming Rule||Resolution of |
|Execution||Initialize and Terminate Functions||Function Naming Rule|
|Periodic and Aperiodic Functions||Function Naming Rule|
|Service Interfaces||Receiver||Function Naming Rule||Inport block name|
|Sender||Function Naming Rule for Value||Outport block name|
|Function Naming Rule for Reference|
|Data Transfer||Receiver Function Naming Rule||Data transfer signal name, such as
|Sender Function Naming Rule|
|Functions||Function Customization Template||Header File||Name of the function|
|Internal Functions||Subcomponent Functions||Function Naming Rule||Associated function element, such as the name of the port for an exported function|
|Shared Utility Functions||Function Naming Rule||Name of the function|
|Memory||Storage Class||Header File||Model or subsystem with which the data is associated|
|Definition File||Model or subsystem with which the data is associated|
|Type Name||Model or subsystem with which the data is associated|
|Instance Name||Model or subsystem with which the data is associated|
|Name of Getter||Associated data element|
|Name of Setter||Associated data element|
Control Case by Using Token Decorators
On the Code Generation > Identifiers pane, you can use token decorators to control the case of generated
identifiers. Place a decorator immediately after the target token and enclose the decorator
in square brackets
[ ]. For example, you can set Global
$R[uL]$N$M, which capitalizes the first letter
of the model name and forces the remaining characters in the model name to lowercase.
The table shows how to manipulate the expansion of the
$R token for a
model whose name is
|Desired Expansion||Description||Token and Decorator|
|First letter of model name is uppercase. Remaining characters are not modified.|
|First letter of model name is uppercase. Remaining characters are lowercase.|
|All characters are uppercase.|
|All characters are lowercase.|
|First letter of model name is lowercase. Remaining characters are uppercase.|
|First letter of model name is lowercase. Remaining characters are not modified.|
When you use a decorator, the code generator removes the underscore character
_) that appears between tokens by default. However, you can append
each decorator with an underscore:
$R[U_]$N. For example, if you set the
Global variables parameter to
$R[u_]$N[uL]$M for a
modelName and a
DW, the result is
Modify Identifiers by Using Regular Expression Decorators
To modify identifiers in ways other than by case, you can use regular expressions. To
use a regular expression, enclose the decorator in double quotes. A regular expression
decorator contains two regular expressions separated by a forward slash. The code generator
searches for substrings of the token that match the first regular expression and replaces
those substrings using the second regular expression. For example, the following identifier
naming rule takes the root model name
$R and replaces instances of
$R["a/b"]. The code
generator interprets regular expression syntax in a way that is consistent with the
function, except regular expression
decorators do not support dynamic expressions, which use MATLAB® commands.
You can configure a regular expression decorator by appending a list of options to the
end of the decorator. Separate the options from the second regular expression by using a
forward slash and separate the options from each other by using the pipe character. For
example, the following identifier naming rule takes the root model name
$R and replaces only the first instance of
b, ignoring case:
|Category||Option||Description||Example Decorator||Example Input||Example Output|
|Match/replace occurrence||Match and replace the expression as many times as possible.||
|Match and replace the expression only once.||
|Case matching||Match letter case.||
|Ignore letter case.||
|Ignore letter case while matching but preserve the case of corresponding characters in the original text while replacing.||
|Empty matching||Ignore zero-length matches.||
|Include zero-length matches.||
If a regular expression decorator results in uncompilable code, the code generator produces an error before building the code.
Regular Expression Limitations
Regular expression decorators are applicable only to
regexprep, regular expression decorators do not support dynamic expressions, which use MATLAB commands.
Regular expression decorators are not supported for function mappings in the Code Mappings editor.
The code generator cannot evaluate whether a regular expression is valid. An invalid regular expression does not result in a change to the affected token and does not produce a warning.
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.
model = 'IdentifierFormatting'; open_system(model)
The model contains two subsystems,
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 token expands to the model name,
$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
_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 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_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
Restore the Subsystem methods configuration parameter to the default value of
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);
$N token expands to a unique value for each subsystem, the code generator does not generate name-mangling text.