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 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.

Using the Parameter Writer Block

The following example shows how to use the Parameter Writer block to change a parameter value for a Gain block.

  1. 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.

  2. 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.

  3. Open the Gain block parameter dialog. Set Gain to Parameter_1.

  4. 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.

  5. 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.

  6. Double-click the Initialize Function block. The block is preconfigured with a State Writer block.

  7. 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.

  8. 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.

  9. 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.

  10. Click the ParamRw_Top tab. The Model block displays a badge indicating a value is written to Parameter_1.

See Also

Blocks

Related Topics