Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Parameter Objects

This section discusses how to use parameter objects in code generation.

Configuring Parameter Objects for Code Generation

In configuring parameter objects for code generation, you use the following code generation and parameter object properties:

Effect of Storage Classes on Code Generation for Parameter Objects

The Real-Time Workshop product generates code and storage declarations based on the RTWInfo.StorageClass property of the parameter object. The logic is as follows:

See the table in Controlling Parameter Object Code Generation Using the Model Explorer for examples of code generated for possible settings of RTWInfo.StorageClass.

Controlling Parameter Object Code Generation with Typed Commands

In this section, the Gain block computations of the model shown in the next figure are used as an example of how the Real-Time Workshop build process generates code for a parameter object.

Model Using Parameter Object Kp As Block Parameter

In this model, Kp sets the gain of the Gain block.

To configure a parameter object such as Kp for code generation:

  1. Instantiate a Simulink.Parameter object called Kp. In this example, the parameter object is an instance of the example class SimulinkDemos.Parameter, which is provided with the Simulink product.

    Kp = Simulink.Parameter
    Kp =
    Simulink.Parameter
              Value: 5
            RTWInfo: [1x1 Simulink.ParamRTWInfo]
        Description: ''
           DataType: 'auto'
                Min: -Inf
                Max: Inf
           DocUnits: ''
         Complexity: 'real'
         Dimensions: '[1x1]'

    Make sure that the name of the parameter object matches the desired block parameter in your model. This ensures that the Simulink engine can associate the parameter name with the correct object. In the preceding model, the Gain block parameter Kp resolves to the parameter object Kp.

  2. Set the object properties you need. You can do this by using the Model Explorer, or you can assign properties by using MATLAB commands, as follows:

    • To specify the Value property, type

      Kp.Value = 5.0;
      
    • To specify the storage class of for the parameter, set the RTWInfo.StorageClass property, for example:

      Kp.RTWInfo.StorageClass = 'ExportedGlobal';
      

    The RTWInfo parameters are now

    Kp.RTWInfo
    Simulink.ParamRTWInfo
              StorageClass: 'ExportedGlobal'
                     Alias: ''
        CustomStorageClass: 'Default'
          CustomAttributes: [1x1 
    SimulinkCSC.AttribClass_Simulink_Default]
    

Controlling Parameter Object Code Generation Using the Model Explorer

If you prefer, you can create and modify attributes of parameter objects using the Model Explorer. This lets you see all attributes of a parameter in a dialog box, and alleviates the need to remember and type field names. Do the following to instantiate Kp and set its attributes from Model Explorer:

  1. Choose Model Explorer from the View menu.

    Model Explorer opens or activates if it already was open.

  2. Select Base Workspace in the Model Hierarchy pane.

  3. Select Simulink Parameter from the Add menu.

    A new parameter named Param appears in the Contents pane.

  4. To set Kp.Name in the Model Explorer:

    1. Click the word Param in the Name column to select it.

    2. Rename it by typing Kp in place of Param.

    3. Click Return.

  5. To set Kp.Value in Model Explorer:

    1. Select the Value field at the top of the Dialog pane.

    2. Type 5.0.

    3. Click the Apply button.

  6. To set the Kp.RTWInfo.StorageClass in Model Explorer:

    1. Click the Storage class menu and select ExportedGlobal, as shown in the next figure.

    2. Click Apply.

The following table shows the variable declarations for Kp and the code generated for the Gain block in the model shown in the preceding model, with the Inline parameters and Eliminate superfluous local variables (Expression folding) check boxes selected (which includes the gain computation in the output computation). An example is shown for each possible setting of RTWInfo.StorageClass. Global structures include the model name (symbolized as model_ or _model).

StorageClass Property

Generated Variable Declaration
and Code

Auto

model_Y.Out1 = rtb_u * 5.0;

SimulinkGlobal

struct _Parameters_model {
  real_T Kp;
}
.
.
Parameters_model model_P = {
  5.0
};
.
.
model_Y.Out1 = rtb_u * model_P.Kp;

ExportedGlobal

extern real_T Kp;
.
.
real_T Kp = 5.0;
.
.
model_Y.Out1 = rtb_u * Kp;

ImportedExtern

extern real_T Kp;
.
.
model_Y.Out1 = rtb_u * Kp;

ImportedExternPointer

extern real_T *Kp;
.
.
model_Y.Out1 = rtb_u * (*Kp);

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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