Modify Identifier Format Tokens by Using Token Decorators - MATLAB & Simulink (original) (raw)

Main Content

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 anduL. 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 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. 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 byDW in a model named modelName, the result isModelName_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 ofa 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 ofa 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 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 Decorator Limitations

See Also

regexprep

Topics