Identifier Format Control
You can customize generated identifiers by specifying theIdentifier format controlparameters on theCode Generation > Identifierspane 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$R
token.
The macro can include:
Valid tokens, which are listed inIdentifier Format Tokens. You can use or omit tokens depending on what you want to include in the identifier name. TheShared utilities identifier formatparameter requires you to specify the checksum token,
$C
. The other parameters require the mangling token,$M
. For more information, seeControl Name Mangling in Generated Identifiers. The mangling token is subject to the use and ordering restrictions noted inIdentifier Format Control Parameter Values.Token decorators, which are listed inControl 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 inIdentifier 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 (_
) character.
Identifier Format Tokens
Token | Description |
---|---|
$C |
This token is required forShared utilities identifier format. If the identifier exceeds theMaximum identifier length, the code generator inserts an 8-character checksum to avoid naming collisions. The position of the |
$M |
This token is required. If necessary, the code generator inserts name-mangling text to avoid naming collisions. Modify checksum character length by usingShared checksum lengthparameter. The position of the |
$U |
插入text that you specify for the |
$F |
插入method name (for example, |
$N |
插入name of object (block, signal or signal object, state, parameter, shared utility function or parameter object) for which identifier is being generated. |
$R |
插入root model name into identifier, replacing unsupported characters with the underscore ( Note:This token replaces thePrefix model name to global identifiersoption in previous releases. |
$H |
插入tag indicating system hierarchy level. For root-level blocks, the tag is the text Note:This token replaces theInclude System Hierarchy Number in Identifiersoption in previous releases. |
$A |
插入data type acronym (for example, Note:This token replaces theInclude data type acronym in identifieroption in previous releases. |
$I |
For example, |
$G |
插入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 theHeader File对于存储类in the Embedded Coder®Dictionary. |
$E |
插入the file type.
This token is required forHeader filesandSource files. |
Identifier Format Control Parameter Valueslists the default macro value, the supported tokens, and the applicable restrictions for eachIdentifier format controlparameter.
Identifier Format Control Parameter Values
Parameter | Default Value | Supported Tokens | Restrictions |
---|---|---|---|
Global variables | $R$N$M |
$M ,$R ,$G ,$N ,$U |
$F ,$H ,$A ,$E , and$I are not allowed. |
Global types | $N$R$M_T |
$M ,$R ,$G ,$N ,$U |
$F ,$H ,$A ,$E , and$I are not allowed. |
Field name of global types | $N$M |
$M ,$N ,$H ,$A ,$U |
$R ,$F ,$G ,$E , and$I are not allowed. |
Subsystem methods | $R$N$M$F |
$M ,$R ,$N ,$H ,$F ,$U |
$F and$H are empty for Stateflow®functions;$A ,$G ,$E , and$I are not allowed. |
Subsystem method arguments | rt$I$N$M |
$M ,$N ,$I ,$U |
$R ,$F ,$H ,$G ,$E , and$A are not allowed. |
Local temporary variables | $N$M |
$M ,$R ,$N ,$A ,$U |
$F ,$H ,$G ,$E , and$I are not allowed. |
Local block output variables | rtb_$N$M |
$M ,$N ,$A ,$U |
$R ,$F ,$H ,$G ,$E , and$I are not allowed. |
Constant macros | $R$N$M |
$M ,$R ,$N ,$U |
$F ,$H ,$A ,$G ,$E , and$I are not allowed. |
Shared utilities identifier format | $N$C |
$N ,$C ,$R ,$U |
$C is required.$M ,$F ,$H ,$A ,$G ,$E , and$I are not allowed. |
EMX array utility functions identifier format |
emx$M$N |
$M ,$N ,$R |
$C ,$U ,$F ,$H ,$A ,$G ,$E , and$I are not allowed. |
EMX array types identifier format |
emxArray_$M$N |
$M ,$N ,$R |
$C ,$U ,$F ,$H ,$A ,$G ,$E , and$I are not allowed. |
Header files | $R$E |
$R ,$U ,$E |
$C ,$M ,$N ,$F ,$H ,$A ,$G , and$I are not allowed. |
Source files | $R$E |
$R ,$U ,$E |
$C ,$M ,$N ,$F ,$H ,$A ,$G , and$I are not allowed. |
Data files | $R_data |
$R ,$U |
$C ,$M ,$N ,$F ,$H ,$A ,$G ,$E , and$I are not allowed. |
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.
For limitations that apply toIdentifier format controlparameters, seeExceptions to Identifier Formatting ConventionsandIdentifier Format Control Parameters Limitations.
Control Case by Using Token Decorators
On theCode Generation>Identifierspane, 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 setGlobal variablesto$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 ismodelName
.
Desired Expansion | Description | Token and Decorator |
---|---|---|
ModelName |
First letter of model name is uppercase. Remaining characters are not modified. | $R[u] |
Modelname |
First letter of model name is uppercase. Remaining characters are lowercase. | $R[uL] |
MODELNAME |
All characters are uppercase. | $R[U] |
modelname |
All characters are lowercase. | $R[L] |
mODELNAME |
First letter of model name is lowercase. Remaining characters are uppercase. | $R[lU] |
modelName |
First letter of model name is lowercase. Remaining characters are not modified. | $R[l] |
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 theGlobal variablesparameter to$R[u_]$N[uL]$M
for a model namedmodelName
and aDWork
structure represented byDW
, the result isModelName_Dw
.
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 ofa
withb
:$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®命令。regexprep
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
,只替换第一个这个ce ofa
withb
, ignoring case:$R["a/b/once|ignorecase"]
.
Category | Option | Description | Example Decorator | Example Input | Example Output |
---|---|---|---|---|---|
Match/replace occurrence | all (default) |
Match and replace the expression as many times as possible. |
["a/-/all"] |
afabcswbc |
-f-bcswbc |
once |
Match and replace the expression only once. |
["a/-/once"] |
afabcswbc |
-fabcswbc |
|
N |
WhereN is an integer, match and replace theN th occurrence of a match. |
[" a / - / 2 "] |
afabcswbc |
af-bcswbc |
|
Case matching | matchcase (default) |
Match letter case. |
["aBcD/wXyZ/matchcase"] |
abcdABCD |
abcdABCD |
ignorecase |
Ignore letter case. |
["aBcD/wXyZ/ignorecase"] |
abcdABCD |
wXyZwXyZ |
|
preservecase |
Ignore letter case while matching but preserve the case of corresponding characters in the original text while replacing. |
["aBcD/wXyZ/preservecase"] |
abcdABCD |
wxyzWXYZ |
|
Empty matching | noemptymatch (default) |
Ignore zero-length matches. |
["^/prefix/noemptymatch"] |
abcdABCD |
abcdABCD |
emptymatch |
Include zero-length matches. |
["^/prefix/emptymatch"] |
abcdABCD |
prefixabcdABCD |
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
$G
,$N
, and$R
tokens.Unlike
regexprep
, regular expression decorators do not support dynamic expressions, which use MATLAB commands.Regular expression decorators are not supported for function mappings in theCode Mappingseditor.
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 theIdentifier format controlparameters on theCode Generation > Identifierspane in the Configuration Parameters dialog box. To maintain model traceability, it is important that incremental revisions to a model have minimal impact on the identifier names that appear in the generated code. This example shows how to use name mangling by specifying theIdentifier format controlparameters to minimally impact the identifier names.
model='rtwdemo_symbols'; open_system(model)
% Cleanuprtwdemoclean; close_system(model,0)