Tune Parameter Structures with Simulink Real-Time Explorer

To reduce the number of workspace variables you must maintain and avoid name conflicts, you can group closely related parameters into structures (see Organize Related Block Parameter Definitions in Structures (Simulink)).

In this example, the initial model xpcosc has four parameters that among them determine the shape of the output waveform.

BlockParameterStructure Field ExpressionInitial Value

Signal Generator

Freq

spkp.sg_freq

20

Gain

Gain

spkp.g_gain

1000^2

Gain1

Gain

spkp.g1_gain

2*0.2*1000

Gain2

Gain

spkp.g2_gain

1000^2

Create Parameter Structure

  1. Open model xpcosc, and save a copy of the model to a working folder.

  2. Open the Base Workspace in the Model Explorer. On the Modeling tab, click Base Workspace. .

  3. Click the Add Simulink Parameter button .

  4. In the Name column, type the name spkp.

  5. In the Storage class field, select ExportedGlobal.

  6. In the Value field, type as one line:

    struct('sg_freq',20, 'g2_gain',1000^2, ...
         'g1_gain',2*0.2*1000, 'g_gain',1000^2)

    The field values duplicate the literal values in the dialog boxes. To change the field values, in row spkp, click the Value cell and click the Edit button .

  7. Click Apply.

  8. Save the model as ex_slrt_osc_struct. On the Simulation tab, from Save, click Save As.

Replace Block Parameters with Parameter Structure Fields

  1. In the Signal Generator block, replace the value of parameter Frequency with spkp.sg_freq.

  2. In the Gain block, replace the value of parameter Gain with spkp.g_gain.

  3. In the Gain1 block, replace the value of parameter Gain with spkp.g1_gain.

  4. In the Gain2 block, replace the value of parameter Gain with spkp.g2_gain.

Tune Parameters in a Parameter Structure

  1. Build and download the model to your target computer.

  2. Open Simulink Real-Time Explorer. In the Real-Time tab, click Prepare > SLRT Explorer.

  3. In the real-time application properties, set the Stop Time parameter to Inf.

  4. Create and configure a host scope:

    1. In the Model Hierarchy node, right-click the model and open View Signals.

    2. Add a host scope ().

    3. Drag the signals Integrator1 and Signal Generator to the scope.

    4. Start the scope ().

    5. View the scope ().

  5. In the Model Hierarchy node, right-click the model and open View Block Parameters.

  6. Open the Values text box for spkp(1).g1_gain.

  7. Start the real-time application ().

  8. In the Values text box for spkp(1).g1_gain, change the value to 800, click outside of the box, and click the Apply parameter value(s) changes button .

  9. Stop the real-time application ().

Save and Load Parameter Structure

  1. In Model Explorer, right-click row spkp.

  2. Click Export selected and save the variable as ex_slrt_osc_struct.mat.

To load the parameter structure when you open the model, add a load command to the PreLoadFcn callback. To remove the parameter structure from the workspace when you close the model, add a clear command to the CloseFcn callback. For more information, see Model Callbacks (Simulink).

Related Topics