Initialize and Reset Parameter Values
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, Reinitialize Function, and Reset Function blocks can respond to events while the Parameter Writer block can change parameter values. The Parameter Writer block can change parameters in these ways:
Writing directly to a parameter of a block that is tunable during simulation
Writing to an instance parameter belonging to a Model block referencing a model, thus affecting block parameters inside the referenced model
Writing to a masked subsystem parameter, thus affecting block parameters inside the subsystem
Writing to a model workspace variable, thus affecting block parameters that are based on that variable
Adding a Parameter Writer Block
You can use a Parameter Writer block inside an Initialize Function, Reinitialize Function, or Reset Function block to write a parameter value in response to events. You can also use a Parameter Writer block inside a conditionally executed subsystem to write a parameter value whenever the subsystem executes.
A block with a parameter that can be written by a Parameter Writer block is called a parameter owner block. A parameter owner block can be a block with a tunable parameter, a Model block, or a masked subsystem block.
To add a Parameter Writer block to your model, click a parameter owner block
to highlight it. Then, from the current block tab of the Simulink® toolstrip, click Parameter Writer. Alternatively,
click Reader-Writer, then select Parameter Writer
Block
from the list.
Note
The current block tab of the toolstrip might be labeled with the name of the highlighted block, for example, Constant, or it might be labeled Block, depending on the type of block you highlight.
If the parameter owner block has multiple parameters to which the Parameter Writer block can write, then the Parameter Writer block dialog box opens. Select a parameter from the Parameter Owner Selector Tree.
After you add a Parameter Writer block, a badge and the name of the parameter being written to appear above the parameter owner block. The names of the parameter owner block and the parameter appear next to the Parameter Writer block.
After you add a Parameter Writer block, cut and paste it to move it to the desired location within the model hierarchy.
Alternatively, you can add a Parameter Writer block to your model from the Simulink Library Browser, then select a parameter owner block or a model workspace parameter from the block dialog.
Note
When you create a library block with a Parameter Writer block, you must also include the corresponding parameter owner block in the library block.
Writing to Tunable Block Parameters
A Parameter Writer block can write directly to a parameter of a block that is tunable during simulation. See Which Block Parameters Are Tunable During Simulation? The parameter must take its value from the block dialog box and not from a port. Initial condition parameters are not included. The Parameter Writer block must be in the same model as the block whose parameter it is writing.
Writing to Model Instance Parameters
A Parameter Writer block can change the value of a block parameter inside a referenced model by writing to an instance parameter of the model. This example shows how to use the Parameter Writer block to change a parameter value for a Gain block inside a referenced model.
Create a model with a writable parameter, which is a block parameter that you can define with a model parameter. In the example, add Constant, Gain, and Outport blocks to a new model. Connect the blocks. Save the model with the name
ParamRw_Sub
.Add a Simulink parameter to the Model Workspace. On the Modeling tab, in the Design section, select Model Workspace . From the Model Explorer menu, select Add > Simulink Parameter. Set Name to
Parameter_1
and Value to1
. Select the Argument check box. Click Apply.Open the Gain Block Parameters dialog box. 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 Parameters dialog box. In the Model name box, enterParamRw_Sub
. Select the Instance parameters tab. Set the Value forParameter_1
to1
. The model uses this default value before the Parameter Writer block updates this parameter with a new value.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 Parameters dialog box. From the Parameter Owner Selector Tree, select
Parameter_1
.Open the Constant Block Parameters dialog box. Set Constant value to 3. This value sets 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
forModel_1
.Click the
ParamRw_Top
tab. The Model block displays a badge , indicating a value is written toParameter_1
.
Writing to Masked Subsystem Parameters
A Parameter Writer block can change the value of a block parameter inside a
masked subsystem by writing to a parameter of the subsystem. The mask parameter must
have Evaluate selected and Tunable set to
on
or on
. See Mask Editor Overview. The subsystem can be atomic or
virtual.
Writing to Model Workspace Variables
A Parameter Writer block can directly write the value of a model
workspace variable. The block must be in the same model as the workspace variable, and the
variable must not be configured as a model argument. You can write only to model workspace
variables and not to variables in the base MATLAB® workspace or data dictionaries. The variable written to can be a regular
MATLAB variable or a Simulink.Parameter
object.
To write to a model workspace variable, in the block dialog box, select Model
workspace variable
from Destination
parameter drop-down options. Enter the model workspace variable name in
Workspace variable name field. Note that the
Parameter Owner Selector Tree does not appear.
See Also
Initialize Function | Reinitialize Function | Reset Function | Terminate Function | Event Listener | State Reader | State Writer | Parameter Writer
Related Topics
- Configure Instance-Specific Values for Block Parameters in a Referenced Model
- Using Initialize, Reinitialize, Reset, and Terminate Functions
- Create Test Harness to Generate Function Calls
- Initialize, Reinitialize, Reset, and Terminate Function Limitations