Main Content

Modify Identifier Format Tokens by Using Token Decorators

You can use token decorators to modify the expanded values of identifier format tokens. Identifier format tokens dynamically insert text into identifiers in the generated code. For a list of identifier format tokens and their expanded values, see Identifier Format Control. Use token decorators to generate identifiers that are readable, consistent, and compliant with necessary coding standards.

There are two types of token decorator. Basic token decorators apply capitalization rules to expanded values. Regular expression token decorators apply regular expressions to expanded tokens. Use regular expression token decorators to specify modifications that are more specific than the capitalization rules that basic token decorators support.

To apply a token decorator to a token, place the token decorator text in square brackets ([ and ]) directly after the token. For example, to render the $R token in all caps, use $R[U].

Control Case of Identifiers by Using Basic Token Decorators

Basic token decorators consist of predefined values such as u and uL. This table shows how each basic token decorator affects the expanded token. Assume the name of the root model, which is the expanded value of $R, is modelName.

Desired ExpansionDescriptionToken and Decorator
ModelNameFirst letter of model name is uppercase. Remaining characters are not modified.$R[u]
ModelnameFirst letter of model name is uppercase. Remaining characters are lowercase.$R[uL]
MODELNAMEAll characters are uppercase.$R[U]
modelnameAll characters are lowercase.$R[L]
mODELNAMEFirst letter of model name is lowercase. Remaining characters are uppercase.$R[lU]
modelNameFirst 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. To preserve the underscore, append the decorator with an underscore: $R[U_]$N. For example, if you set the Global variables model configuration parameter to $R[u_]$N[uL]$M for a DWork structure represented by DW in a model named modelName, the result is ModelName_Dw. If you omit the underscore and use the identifier naming rule $R[u]$N[uL]$M, the result is ModelNameDw.

Modify Identifiers by Using Regular Expression Token Decorators

To modify identifiers in ways that basic token decorators do not support, you can use regular expression token decorators. To use a regular expression decorator, 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 regexprep 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 a with b, ignoring case: $R["a/b/once|ignorecase"].

CategoryOptionDescriptionExample DecoratorExample InputExample Output
Match/replace occurrenceall (default)Match and replace the expression as many times as possible.
["a/-/all"]
afabcswbc-f-bcswbc
onceMatch and replace the expression only once.
["a/-/once"]
afabcswbc-fabcswbc
NWhere N is an integer, match and replace the Nth occurrence of a match.
["a/-/2"]
afabcswbcaf-bcswbc
Case matchingmatchcase (default)Match letter case.
["aBcD/wXyZ/matchcase"]
abcdABCDabcdABCD
ignorecaseIgnore letter case.
["aBcD/wXyZ/ignorecase"]
abcdABCDwXyZwXyZ
preservecaseIgnore letter case while matching but preserve the case of corresponding characters in the original text while replacing.
["aBcD/wXyZ/preservecase"]
abcdABCDwxyzWXYZ
Empty matchingnoemptymatch (default)Ignore zero-length matches.
["^/prefix/noemptymatch"]
abcdABCDabcdABCD
emptymatchInclude zero-length matches.
["^/prefix/emptymatch"]
abcdABCDprefixabcdABCD

If a regular expression decorator results in uncompilable code, the code generator produces an error before building the code.

Regular Expression Decorator Limitations

  • Regular expression decorators apply 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.

See Also

Related Topics