Main Content

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.

Block tab of Simulink toolstrip, showing Parameter Writer button. A gain block is highlighted on the canvas.

Block tab of Simulink toolstrip, showing Reader-Writer button with drop-down menu beneath it. List items are Parameter Writer Block and State Reader Block. A Discrete Filter block is highlighted on the canvas.

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.

Parameter Writer block dialog. In Parameter Owner Selector Tree, "Discrete Filter" is highlighted, with "Denominator" and "Numerator" appearing indented below it.

After you add a Parameter Writer block, a diamond shape with a p inside it 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.

Simulink canvas showing Gain block named "Mu" with the word "Gain" and a badge above it, and a Parameter Writer block with "Mu.Gain" appearing next to it

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.

  1. 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 the blocks mentioned in Step 1 and change the Gain block parameter to writable parameter Parameter_1.

  2. 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 to 1. Select the Argument check box. Click Apply.

    Add parameter to Model Workspace.

  3. Open the Gain Block Parameters dialog box. 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.

    Add Initialize Function block and Model block and save the model as ParamRw_Top.

  5. Rename the Model block to Model_1. Open the Model Block Parameters dialog box. In the Model name box, enter ParamRw_Sub. Select the Instance parameters tab. Set the Value for Parameter_1 to 1. The model uses this default value before the Parameter Writer block updates this parameter with a new value.

    Set Instance parameter named Parameter_1 value to 1.

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

    Open the Initialize Function block to configure the block to change Parameter_1 value.

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

    Configure Parameter Writer block to write to model instance parameter Parameter_1.

  8. Open the Constant Block Parameters dialog box. Set Constant value to 3. This value sets 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.

    Contents inside the Initialize Function block shows that when the Initialize Function block executes at the start of simulation, the Parameter Writer block writes a constant value of 3 to Parameter_1.

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

    Contents of ParamRw_Top model.

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.

Mask Editor dialog. Evaluate and Tunable attributes are highlighted.

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.

Block dialog for Parameter Writer block. Select Model workspace variable from Destination parameter drop-down options and mention the variable name in Workspace variable name field.

See Also

| | | | | | |

Related Topics

External Websites