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 $R
token.
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 Control Name Mangling in Generated Identifiers. 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 (_
) character.
Identifier Format Tokens
Token | Description |
---|---|
$C | 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
|
$M | 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 |
$U | Insert text that you specify for the |
$F | Insert method name (for example, |
$N | 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 |
$R | 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. |
$H | 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. |
$A | Insert data type acronym (for example, Note: This token replaces the Include data type acronym in identifier option in previous releases. |
$I |
For example, |
$G | 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. |
$E | Insert the file type.
This token is required for Header files and Source files. |
$X | 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 |
---|---|---|---|
Global variables | $R$N$M | $M ,
$R ,$N ,$U | $F , $H , $A ,
$E , $I , and $G are not
allowed. |
Global types | $N$R$M_T | $M ,
$R ,$N ,$U | $F , $H , $A ,
$E , $I , and $G 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 to Identifier format control parameters, see Exceptions to Identifier Formatting Conventions and Identifier Format Control Parameters Limitations.
$N
Embedded Coder Dictionary Resolution
The $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 $N |
---|---|---|---|---|
Execution | Initialize and Terminate Functions | Function Naming Rule | $R$N | init for Initialize Function blocks,
term for Terminate Function blocks |
Periodic and Aperiodic Functions | Function Naming Rule | $R$N | reset for periodic functions, step for
aperiodic functions | |
Service Interfaces | Receiver | Function Naming Rule | get_$X$N | Inport block name |
Sender | Function Naming Rule for Value | set_$X$N | Outport block name | |
Function Naming Rule for Reference | ||||
Data Transfer | Receiver Function Naming Rule | get_$X$N
| Data transfer signal name, such as
DataTransferAt | |
Sender Function Naming Rule | set_$X$N | |||
Internal Functions | Subcomponent Functions | Function Naming Rule | $R$N | Associated function element, such as the name of the port for an exported function |
Shared Utility Functions | Function Naming Rule | $N$C | Name of the function | |
Memory | Storage Class | Header File | $N.h | Model or subsystem with which the data is associated |
Definition File | $N.c | Model or subsystem with which the data is associated | ||
Type Name | $R$$N$G$M | Model or subsystem with which the data is associated | ||
Instance Name | $G$N$M | Model or subsystem with which the data is associated | ||
Name of Getter | get_$N$M | Associated data element | ||
Name of Setter | set_$N$M | 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
variables to $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 modelName
.
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 the
Global variables parameter to $R[u_]$N[uL]$M
for a
model named modelName
and a DWork
structure
represented by DW
, the result is ModelName_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 of
a
with b
: $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.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
and replaces only the first instance of a
with
b
, 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 | Where N is an integer, match and replace the
N 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 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, 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 the Identifier format control parameters to minimally impact the identifier names.
model='rtwdemo_symbols';
open_system(model)
% Cleanup
rtwdemoclean;
close_system(model,0)