Apply Custom Naming Conventions to Identifiers
This example shows how to apply uniform naming rules for Simulink® data objects, including signals, parameters, and data store memory variables.
When your model uses Simulink data objects from the Simulink
package, identifiers in the generated code copy the names of the objects by default. For example, a Simulink.Signal
object named Speed
appears as the identifier Speed
in generated code.
You can control these identifiers by specifying naming rules that are specific to Simulink data objects. When you specify naming rules for generated code, follow ANSI® C/C++ rules for naming identifiers.
Apply Lowercase Naming Rule
1. Open the model.
model='NameRules';
open_system(model)
2. Open the C Code tab and click View Code.
3. In the Model Configuration Parameters, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. In this model, all three rules are set to Force lower case
.
4. Generate code for the model. The generated file NameRules.c
uses lowercase identifiers for parameters.
/* Exported block parameters */ real_T f1 = 2.0; /* Variable: F1 * Referenced by: '<Root>/Chart' */ real_T g1 = 3.0; /* Variable: G1 * Referenced by: * '<Root>/Chart' * '<S3>/Gain1' */ real_T g2 = 4.0; /* Variable: G2 * Referenced by: '<S3>/Gain2' */ real_T g3 = 5.0; /* Variable: G3 * Referenced by: '<S2>/Gain' */
Apply Naming Rule Using a Function
You can customize identifiers in generated code by defining a MATLAB® function.
1. Write a MATLAB function that returns an identifier by modifying a data object name, then save the function in your working folder. For example, the following function returns an identifier name by appending a data object's data type to that data object's name.
function revisedName = append_text(name, object) % APPEND_TEXT: Returns an identifier for generated % code by appending text to a data object name. % % Input arguments: % name: data object name as spelled in model % object: information about model % % Output arguments: % revisedName: altered identifier returned for use in % generated code. % % DataType = Simulink.data.evalinGlobal(object.modelName,[name,'.DataType']); revisedName = [name,'_',DataType];
2. In the Model Configuration Parameters, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. From the Parameter naming drop-down list, select Custom M-function
.
3. In the M-function field under Parameter naming
, type the name of the file that defines the MATLAB function, append_text.m
, then click Apply.
4. Generate code for the model. The generated code implements the parameter object naming rule in NameRules.c
.
/* Exported block parameters */ real_T F1_auto = 2.0; /* Variable: F1 * Referenced by: '<Root>/Chart' */ real_T G1_auto = 3.0; /* Variable: G1 * Referenced by: * '<Root>/Chart' * '<S3>/Gain1' */ real_T G2_auto = 4.0; /* Variable: G2 * Referenced by: '<S3>/Gain2' */ real_T G3_auto = 5.0; /* Variable: G3 * Referenced by: '<S2>/Gain' */
Specify Naming Rule for Storage Class define
You can specify a naming rule that applies only to Simulink data objects whose storage class you set to Define
.
1. Open the Code Mappings Editor. On the C Code tab, select Code Interface > Default Code Mappings.
2. In the Parameters tab of the Code Mappings Editor, find the data table row labeled External Parameter Objects
. Click the Refresh button in that row.
3. Find the row in the data table that corresponds to the Simulink.Parameter
object G1
, which resides in the base workspace. From the drop-down menu, set the Storage Class for G1
to Define
.
4. In the Model Configuration Properties, navigate to Identifiers > Advanced parameters > Simulink data object naming rules. From the #define naming
drop-down list, select Custom M-function.
5. In the M-function field under #define naming
, type the name of the file that defines the MATLAB function, append_text.m
.
6. From the Parameter naming drop-down list, select Force lower case
, then click Apply.
7. Generate code for the model. The generated file NameRules.h
represents G1
with G1_auto
and K1
with K1_auto
.
/* Definition for custom storage class: Define */ #define G1_auto 3.0 /* Referenced by: * '<Root>/Chart' * '<S3>/Gain1' */ #define K1_auto 6.0 /* Referenced by: '<Root>/Chart' */
Override Data Object Naming Rules
You can override data object naming rules for individual data objects by specifying the Identifier
property. Generated code uses the text that you specify as the identifier to represent the data object, regardless of naming rules.
1. In the Parameters tab of the Code Mappings Editor, find the row in the data table that corresponds to the Simulink.Parameter
object G2
. Right click the row and select Open.
2. In the Property Inspector, go to the Code Generation tab. Specify the Identifier
property as myIdentifier
. Click Apply.
3. Generate code for the model. The generated file NameRules.c
represents G2
with the variable myIdentifier
.
/* Exported block parameters */ real_T f1 = 2.0; /* Variable: F1 * Referenced by: '<Root>/Chart' */ real_T myIdentifier = 4.0; /* Variable: G2 * Referenced by: '<S3>/Gain2' */ real_T g3 = 5.0; /* Variable: G3 * Referenced by: '<S2>/Gain' */