Parameter Writer
Write to block parameter or variable
Libraries:
Simulink /
Signal Routing
Description
A Parameter Writer block changes block parameter and base or model workspace values. Use the Parameter Writer block when you need to change parameter or workspace values at run time.
A Parameter Writer block writes to one of the following:
A block parameter that is tunable during simulation
An instance parameter that belongs to a Model block that references a model
A model workspace variable
A base workspace variable
A variable stored in a Simulink® data dictionary created using
Simulink.data.DictionaryobjectA masked subsystem parameter
A block with a parameter that can be written by a Parameter Writer block is called a parameter owner block.
Note
The value written by the Parameter Writer block is temporarily stored in memory during simulation. The actual values of the parameters or variables remain unchanged.
Identifying Parameter Owner Blocks
You can determine whether a block is a supported parameter owner block using the
IsParamOwnerBlock parameter. Use the
get_param function with the block name or handle,
blk, and the IsParamOwnerBlock
parameter.
get_param(blk,'IsParamOwnerBlock')
The function returns 'on' if the block is a supported parameter
owner block and 'off' otherwise.
A Parameter Writer block and its parameter owner block constitute a set of related blocks. You can select a Parameter Writer block to highlight the related parameter owner block or vice versa. When a related block is highlighted, blocks in the current model that contain the related block are also highlighted. For example, an Initialize Function block is highlighted when it contains a Parameter Writer block that is related to the selected block. The blocks that are highlighted in the model canvas are also highlighted in the miniature map of the block diagram.
To show a related block in an open diagram or new tab, pause on the ellipsis that appears
after selection. Then, in the action bar that appears, click Related
Blocks
. Window focus goes to the open diagram or new tab
that shows the related block.
Configure the Block
Data type — The data type of the input signal of a Parameter Writer block must match the output data type of its parameter owner block. Simulink throws an error if it detects any mismatch between the output data type of a parameter owner block and the input data type of its Parameter Writer block.
Sample Time — Using a Parameter Writer block to write to blocks with constant sample can result in conflicts in both simulation and code generation due to internal optimization around the sample time. Writing to blocks with constant sample time using a Parameter Writer block is not supported when the Parameter Writer block is outside an Initialize Function, a Reset Function, a Reinitialize Function, or a Terminate Function block.
For some solvers, when you use a Parameter Writer block to write to a block with continuous sample time, the block parameter can behave differently than when the parameter value is modeled as a signal.
Scope of Parameter Writer Block
You can use the Parameter Writer block inside a conditionally executed subsystem, in a block with custom routine such as an Initialize Function, a Reset Function, a Reinitialize Function, or a Terminate Function block, in a masked subsystem, or and at a general location in a Simulink model. However, the block is designed to be used inside a conditionally executed subsystem and set up to execute periodically or based on events or certain conditions.
Use in a conditionally executed subsystem — You can use the Parameter Writer block inside a conditionally executed subsystem to improve simulation performance. In such subsystems, the Parameter Writer block executes when a specific condition is met, and it executes before its parameter owner block. For more information about using conditionally executed subsystems, see Conditionally Executed Subsystems and Models.
In this example, the Parameter Writer block is used inside the Enabled Subsystem block.

Use in Initialize Function, Reset Function, Reinitialize Function, and Terminate Function blocks — Use the Parameter Writer block with the Initialize Function, Reinitialize Function, and Reset Function blocks to respond to events. For example, an event could be reading the value from a hardware sensor and then updating a block parameter based on the sensor value.
In this example, Parameter Writer blocks are used within Initialize Function and Reset Function blocks.

Use in a masked subsystem — You can write to a mask parameter of a masked subsystem block using a Parameter Writer block that is outside the masked subsystem. However, this block cannot write to any other block inside the mask. The mask whose value has been written must not contain an initialization code. When the Parameter Writer block changes the mask parameter value at run time, initialization code can cause an error.
If a Parameter Writer block is inside a masked subsystem, the Parameter Writer block and corresponding parameter owner block must have the same parent masked subsystem. This Parameter Writer block cannot write to any other block outside the masked subsystem.
Otherwise, a Parameter Writer block must be in the same mask as either:
The block whose parameter is directly written by the Parameter Writer block, which could be a built-in block such as a Gain block, a Model block, a masked subsystem block, or a User-Defined Functions block such as an S-Function block
The block whose parameter is indirectly written through a workspace variable
When a Parameter Writer block writes values with data type other than
double (the default) to a mask parameter, you must initialize
the mask parameter with a value of the same data type as the value written by the
Parameter Writer block. To initialize the mask parameter,
double-click the block to open the mask dialog box, specify the data type and value
of the mask parameter. Alternatively, to initialize the mask parameter
programmatically, use the MaskValues parameter with the
set_param function. For more information, see Control Masks Programmatically.
In this example, a Parameter Writer block writes a value with data type
single to a Gain block parameter inside a
masked subsystem. The mask parameter is initialized to 1 with
data type single.

Destination of Parameter Writer Block
You can use the Parameter Writer block to change the value of a built-in block parameter directly or indirectly by writing to the parameter via a base workspace variable, model workspace variable, mask parameter, or model instance parameter.
Write to built-in block parameter — Tunable built-in block parameters are generally piece-wise continuous and change less frequently than a signal. If you want to change the value of a run-time tunable parameter periodically or based on events or certain conditions, use the Parameter Writer block.
To write to a block parameter, set Desitation parameter to
Block Parameter. Then, in the Parameter
Owner Selector Tree, select the block parameter whose value you want
to change.
In this example, the Parameter Writer block inside the If Action Subsystem block changes the value of the Gain parameter. The Parameter Writer block executes based on the if-else conditions defined in the If block.

Write to base workspace variable — A base workspace
variable is globally accessible and defined in the MATLAB® workspace. This workspace variable can be a MATLAB variable, a Simulink.Parameter object, or a
Simulink.VariantControl object.
In a model hierarchy, to use a Parameter Writer block to write to a base workspace variable:
You must use the block only from one level of the model hierarchy. Using Parameter Writer blocks at multiple levels of the model hierarchy to write to the same base workspace variable can yield incorrect results.
When you use a Parameters Writer block to write to a base workspace variable at a particular level of model hierarchy, the block can only be used to write to the variable in the scope of that model. You cannot use a Parameter Writer block to write to that variable in another model at another level in the model hierarchy.
To write to a base workspace variable, set Desitation parameter to
Base workspace variable. Then enter the base
workspace variable name in the Workspace variable name
field.
In this example, the Parameter Writer block changes the value of
base workspace variable named gainParam based on a function-call
signal. This gainParam variable represents the
Gain parameter.

Write to model workspace variable — A model workspace variable is only visible in the scope of the model and is initialized only when the model is loaded.
When you write to a model workspace variable using a Parameter Writer block, the variable behaves like a model workspace marked as an argument, which is instance-specific for both simulation and code generation. If a model workspace variable is already marked as an argument, the Parameter Writer block does not write its value.
For code generation, use Simulink.Parameter object with the storage class
set to Auto or Model Default. Avoid using
MATLAB variables, as they can be inlined even when Default
parameter behavior is set to
Tunable.
To write to a model workspace variable, set the Desitation parameter
to Model workspace variable. Then, enter the model
workspace variable name in the Workspace variable name
field.
In this example, the Parameter Writer block changes the value of model
workspace parameter named Param2, which is also used as the
Gain parameter.

Write to model instance parameter — To write to a base or
model workspace variable, a variable created in a
Simulink.data.Dictionary, a mask parameter, or a
Model block instance parameter with a Parameter
Writer block, the variables and the parameters must be used by a
parameter owner block or C MEX S-function with run-time tunable parameters.
To write to a model instance parameter, set Desitation parameter to
Block Parameter. Then, in the Parameter
Owner Selector Tree, select the Model block parameter
whose value you want to change. For more information about how to write to a model
instance parameter, see Writing to Model Instance Parameters.
In this example, the Parameter Writer block writes to the model
instance parameter Param1.

When a Parameter Writer block writes to an instance parameter with the
storage class set to Model default, the code generator
uses the default configuration for Model parameter arguments
when generating code for the parameter. See Specify Instance-Specific Parameter Values for Reusable Referenced Model (Simulink Coder).
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. In addition to custom library blocks created using the workflow mentioned in Create Custom Library, Simulink treats model components created using the Subsystem Reference block as a one-block library.
Debugging Tools
Connectors — Use Parameter Connectors to identify Parameter Writer blocks and their corresponding parameter owner blocks in a model. To visualize the connections:
In the Simulink Toolstrip, on the Debug tab, select Information Overlays > Connectors.
To view the connections, select Parameter Connectors.
In this example, the Parameter Writer blocks inside Initialize Function and Reset Function blocks write to the Gain block parameter. The yellow lines show the connection between the Parameter Writer and the Gain block.

Execution order — When you use a Parameter Writer block outside of an Initialize Function, a Reset Function, a Reinitialize Function, or a Terminate Function block and other asynchronous functions, the Parameter Writer block executes in the same task as its corresponding parameter owner block. To check the execution order, on the Debug tab, select Information Overlays > Execution Order.The Execution Order viewer opens in a pane on the right side of the Simulink Editor and displays information based on the latest compiled execution order.
Examples
Limitations
A Parameter Writer block cannot be placed in a Simulink function.
Ports
Input
Parameters
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Tips
Value range — When you use a Parameter Writer block to write to a parameter with a specified range of values, the Pramater Writer block must be configured to accept values within that range. If you attempt to use a value outside the range, you get a simulation error.
Performance— In general, updating a parameter involves a lot of evaluation and validation. To improve performances:
Avoid writing to a nested block parameter. This slows down the simulation. For example, avoid writing to a mask parameter that is used by another mask or another model instance parameter.
Write to a model workspace variable instead of a model instance parameter from a top-level model in a model hierarchy.
Sources of parameter change— When you use a Parameter Writer block to update a parameter during simulation, avoid using any other source of change for that parameter. This might result into conflict leading to incorrect simulation results.
Code generation — To generate code from models using
a Parameter Writer block, you must set the Code Generation > Optimization > Default parameter behavior to Tunable. When writing to workspace variable,
use a Simulink.Parameter object so that it is not optimized and
inlined in the generated code.
Extended Capabilities
Version History
Introduced in R2016bSee Also
Blocks
- Initialize Function | Reinitialize Function | Reset Function | Terminate Function | Event Listener | State Reader | State Writer | Model | S-Function | Subsystem


