During a simulation, you can respond to an event such as reading an environment sensor value, and then update an algorithm with a new parameter value.
The Initialize Function and Reset Function blocks can respond to events while the Parameter Writer block can change parameter values. The Parameter Writer block does not write directly to block parameter values. Instead, it changes block parameter values in a referenced model by writing to instance parameters belonging to the Model block.
The following example shows how to use the Parameter Writer block to change a parameter value for a Gain block.
Create a model with a writable parameter, that is a block parameter you can define
with a model parameter. In the example, add a Constant,
Gain, and Outport block to a new model. Connect blocks.
Save the model with the name ParamRw_Sub
.
Add a Simulink® parameter to the Model Workspace. On the Modeling tab
and from the Design section, select Model
Workspace
. From the Model Explorer menu, select Add > Simulink Parameter. Set Name to
Parameter_1
and
Value to 1
. Select the
Argument check box. Click Apply.
Open the Gain block parameter dialog. Set Gain
to Parameter_1
.
Create a model that initializes the parameter. Add an Initialize
Function and Model block to a new model. Save the model with
the name ParamRw_Top
.
Rename the Model block to Model_1
. Open the
Model block parameter dialog box. In the Model
name box, enter ParamRw_Sub
. Select the Instance
parameters tab. Set the Value for Parameter_1
to
1
. This is the default value the model uses before the
Parameter Writer block updates this parameter with a new value.
If the Model block is at a lower hierarchical level than the Parameter Writer block, select the Argument check box to promote the parameter to a higher level and make it visible to the Parameter Writer block.
Double-click the Initialize Function block. The block is preconfigured with a State Writer block.
Replace the State Writer block with a Parameter Writer
block. Open the Parameter Writer block parameter dialog box. From the
Parameter Owner Selector Tree, select
Parameter_1
.
Open the Constant block parameter dialog box. Set Constant value to 3. This is the value for setting the gain with the Parameter Writer block.
Click OK to close the dialog. The Parameter
Writer block displays a label indicating it writes to the model instance
parameter Parameter_1
for Model_1
.
Click the ParamRw_Top
tab. The Model block
displays a badge indicating a value is written to Parameter_1.