Tune Parameter Structures by Using 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.
In this example, the initial model slrt_ex_osc
has four parameters that
determine the shape of the output waveform.
Block | Parameter | Structure Field Expression | Initial Value |
---|---|---|---|
| Freq |
|
|
| Gain |
|
|
| Gain |
|
|
| Gain |
|
|
Create Parameter Structure
This procedure groups some closely related parameters into structures.
Open model slrt_ex_osc
, and save a copy of the model to a
working folder.
Open the Base Workspace in the Model Explorer. On the Modeling tab, click Base Workspace.
Click Add Simulink Parameter
.
In the Name column, type the name
spkp
.
In the Storage class field, select
ExportedGlobal
.
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 Edit
.
Click Apply.
Save the model as slrt_ex_osc_struct
. On the
Simulation tab, from Save, click
Save As.
Replace Block Parameters with Parameter Structure Fields
In the
Signal Generator
block, replace the value of parameter Frequency withspkp.sg_freq
.In the
Gain
block, replace the value of parameter Gain withspkp.g_gain
.In the
Gain1
block, replace the value of parameter Gain withspkp.g1_gain
.In the
Gain2
block, replace the value of parameter Gain withspkp.g2_gain
.
Save and Load Parameter Structure
In Model Explorer, right-click row
spkp
.Click Export selected and save the variable as
slrt_ex_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.
Tune Parameters in a Parameter Structure
If you have not completed the steps in Create Parameter Structure, Replace Block Parameters with Parameter Structure Fields, and Save and Load Parameter Structure, you can start by using the completed model.
To open the model, in the MATLAB Command Window, type:
open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_struct')); load(fullfile(matlabroot, 'toolbox', 'slrealtime', ... 'examples', 'slrt_ex_osc_struct.mat'));
Build and download the model to your target computer.
Open Simulink Real-Time Explorer. In the Real-Time tab, click Prepare > SLRT Explorer.
Set the real-time application
Stop Time to Inf
.
Click the Parameters tab.
Start the real-time application.
Open the Simulation Data Inspector and view the signals from the real-time application.
In the Values text box for
spkp(1).g1_gain
, change the value to 800
and press Enter.
Observe the change to the signals in the Simulation Data Inspector.
Stop the real-time application.