Applying Naming Rules to Identifiers Globally

Overview

Signal and parameter names appear on a Simulink model. The same names appear as data objects on the Model Explorer. By default, these names are replicated exactly in the generated code. For example, "Speed" on the model (and workspace) appears as the identifier "Speed" in the code, by default. But you can change how they appear in the code. For example, if desired, you can change "Speed" to SPEED or speed. Or, you can choose to use a different name altogether in the generated code, like MPH. The only restriction is that you follow ANSI®[1] C/C++ rules for naming identifiers.

There are two ways of changing how a signal name or parameter name is represented in the generated code. You can do this globally, by following the procedure in this section. This procedure makes selections on the Configuration Parameters dialog box to change all of the names when code generation occurs, according to the same rule. Or, you can change the names individually by following the steps described in Setting Property Values for Simulink® Data Objects. The relevant field in that procedure is Alias on the Model Explorer.

If the Alias field is empty, the naming rule that you select on the Configuration Parameters dialog box applies to all data objects. But if you do specify a name in the Alias field, this overrides the naming rule for that data object. The table below illustrates these cases. The table assumes that you selected Force lower case as the naming rule. But with the information provided, you can determine how any of the naming rules works for an mpt data object or a Simulink data object (Force upper case, Force lower case, or Custom M-function).

Naming Rules and Alias Override (Global Change of Force Lower Case Rule)

Name of Data Object in ModelName in Alias FieldPackageResult in Generated Code

A

Simulink or mpt

a

A

D

Simulink or mpt

D

Specifying Simulink® Data Object Naming Rules

You specify Simulink data object naming rules on the Real-Time Workshop > Symbols pane of the Configuration Parameters dialog box. To access that pane,

  1. Open an ERT-based model.

  2. Open the Configuration Parameters dialog box from the Simulation menu or Model Explorer.

  3. Open the Real-Time Workshop > Symbols pane. See the subpane Simulink data object naming rules.

Notice the preconfigured settings on this pane. If all of these are acceptable as is, proceed to Creating User Data Types. Otherwise, follow the procedures below, as desired, to change signal names, parameter names, or parameter names that you want to use in a #define preprocessor directive. MPF Panes on the Configuration Parameters Dialog Box describes all fields on this pane and their possible settings for these procedures.

Defining Rules That Change All Signal Names

This procedure allows you to change all of the model's signal names, using the same rule. The new names will appear as identifiers in the generated code:

  1. On the Signal naming field, click the desired selection. (MPF Elements on Configuration Parameters Panes, explains the possible selections, under the Symbols pane.) The default is None. If you selected Custom M-function, go to the next step. Otherwise, click Apply and then generate and inspect code.

  2. Write a function in M-code that changes all occurrences of signal names in the model to appear the way you want as identifiers in the generated code. (An example is shown in Defining Rules That Change All Parameter Names.)

  3. Save the function as a .m file in any folder that is in the MATLAB® path.

  4. In the M-function field under Signal naming, type the name of the file you saved in the previous step.

  5. Click Apply and then generate and inspect code.

Defining Rules That Change All Parameter Names

This procedure allows you to change all of the model's parameter names, using the same rule. The new names will appear as identifiers in the generated code:

  1. In the Parameter naming field, click the desired selection. (MPF Panes on the Configuration Parameters Dialog Box, explains the possible selections, under the Symbols pane.) The default is None. If you selected Custom M-function, go to the next step. Otherwise, click Apply, and proceed to Defining Rules That Change All Signal Names.

  2. Write a function in M-code that changes all occurrences of parameter names in the model to appear the way you want as identifiers in the generated code. For example, the code below changes all parameter names as necessary to make their first letter uppercase, and their remaining letters lowercase.

    function
    revisedName = initial_caps_only(name, object)
    % INITIAL_CAPS_ONLY: User-defined naming rule causing each
    % identifier in the generated code to have initial cap(s).
    %
    % name: name as spelled in model.
    % object: the object of name; includes name's properties.
    %
    % revisedName: manipulated name returned to MPT for the
    code.
    %
    %
    :
    revisedName = [upper(name(1)),lower(name(2:end))];
    :
  3. Save the function as a .m file in any folder that is in the MATLAB path.

  4. In the M-function field under Parameter naming, type the name of the file you saved in the previous step.

  5. Click Apply and then define rules that apply to all signal names.

Defining Rules That Change All #defines

This procedure allows you to change all of the model's parameter names whose storage class you selected as Define in Creating mpt Data Objects with Data Object Wizard, using the same rule. The new names will appear as identifiers in the generated code:

  1. In the #define naming field, click the desired selection. (MPF Panes on the Configuration Parameters Dialog Box, explains the possible selections, under the Symbols pane.) The default is None. If you select Custom M-function, go to the next step. Otherwise, click Apply and proceed to Defining Rules That Change All Parameter Names.

  2. Write a function in M-code that changes all occurrences of the parameter name whose storage class you specified as Define in Creating mpt Data Objects with Data Object Wizard so that it appears the way you want as an identifier in the generated code. (An example is shown below.)

  3. Save the function as a .m file in any folder that is in the MATLAB path.

  4. In the M-function field under #define naming, type the name of the file you saved in the previous step.

  5. Click Apply and then define rules that change all parameter names.


[1] ANSI is a registered trademark of the American National Standards Institute, Inc.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS