Main Content

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

The macro can include:

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$Ctoken in theIdentifier format controlparameter specification determines the position of the checksum in the generated identifier. For example, if you use the specification$N$C, the checksum is appended to the end of the identifier. This token is available only for shared utilities.

$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$Mtoken in theIdentifier format controlparameter specification determines the position of the name-mangling text in the generated identifier. For example, if you use the specification$R$N$M, the name-mangling text is appended (if required) to the end of the identifier. For more information, seeControl Name Mangling in Generated Identifiers.

$U

插入text that you specify for the$Utoken. Use theCustom token textparameter to specify this text.

$F

插入method name (for example,_Updatefor update method). This token is available only for subsystem methods.

$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 (_) character. When you use referenced models, this token is required in addition to$M(seeAvoid Identifier Name Collisions with Referenced Models).

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 textroot_. For blocks at the subsystem level, the tag is of the formsN_.Nis a unique system number assigned by the Simulink®software. This token is available only for subsystem methods and field names of global types.

Note:This token replaces theInclude System Hierarchy Number in Identifiersoption in previous releases.

$A

插入data type acronym (for example,i32for integers) to signal and work vector identifiers. This token is available for local block output variables, local temporary variables, and field names of global types.

Note:This token replaces theInclude data type acronym in identifieroption in previous releases.

$I
  • 插入uif the argument is an input.

  • 插入yif the argument is an output.

  • 插入uyif the argument is an input and output.

For example,rtu_for an input argument,rty_for an output argument, andrtuy_for an input and output argument. This token is available only for subsystem method arguments.

$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.$Erepresents these instances of file types:

  • capi

  • capi_host

  • dt

  • testinterface

  • private

  • types

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$Iare not allowed.
Global types $N$R$M_T $M,$R,$G,$N,$U $F,$H,$A,$E, and$Iare not allowed.
Field name of global types $N$M $M,$N,$H,$A,$U $R,$F,$G,$E, and$Iare not allowed.
Subsystem methods $R$N$M$F $M,$R,$N,$H,$F,$U $Fand$Hare empty for Stateflow®functions;$A,$G,$E, and$Iare not allowed.
Subsystem method arguments rt$I$N$M $M,$N,$I,$U $R,$F,$H,$G,$E, and$Aare not allowed.
Local temporary variables $N$M $M,$R,$N,$A,$U $F,$H,$G,$E, and$Iare not allowed.
Local block output variables rtb_$N$M $M,$N,$A,$U $R,$F,$H,$G,$E, and$Iare not allowed.
Constant macros $R$N$M $M,$R,$N,$U $F,$H,$A,$G,$E, and$Iare not allowed.
Shared utilities identifier format $N$C $N,$C,$R,$U $Cis required.$M,$F,$H,$A,$G,$E, and$Iare not allowed.
EMX array utility functions identifier format
emx$M$N $M,$N,$R $C,$U,$F,$H,$A,$G,$E, and$Iare not allowed.
EMX array types identifier format
emxArray_$M$N $M,$N,$R $C,$U,$F,$H,$A,$G,$E, and$Iare not allowed.
Header files $R$E $R,$U,$E $C,$M,$N,$F,$H,$A,$G, and$Iare not allowed.
Source files $R$E $R,$U,$E $C,$M,$N,$F,$H,$A,$G, and$Iare not allowed.
Data files $R_data $R,$U $C,$M,$N,$F,$H,$A,$G,$E, and$Iare not allowed.

Non-ERT-based targets (such as the GRT target) implicitly use a default$R$N$Mspecification. 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$Rtoken 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]$Mfor a model namedmodelNameand aDWorkstructure 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$Rand replaces instances ofawithb:$R["a/b"]. The code generator interprets regular expression syntax in a way that is consistent with theregexprepfunction, except regular expression decorators do not support dynamic expressions, which use MATLAB®命令。

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 ofawithb, 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 WhereNis an integer, match and replace theNth 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$Rtokens.

  • Unlikeregexprep, 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)