Main Content

Parameter Writer

Write to block parameter or variable

  • Parameter Writer block icon.

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

  • A 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 Related block button.. Window focus goes to the open diagram or new tab that shows the related block.The Related Blocks button shows that the Parameter Writer block inside the enabled subsystem writes to the mask parameter of the masked subsystem.

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.

Parameter Writer block is used inside an enabled subsystem and executes before the Gain block which is the parameter owner 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.

Parameter Writer block writes to Gain block parameter.

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.

The mask parameters dialog box shows the Filter Coefficient parameter with a value of 1 and 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.

The Paramater Writer blocks inside the if action subsystems writes to the Gain parameter.

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.

The Paramater Writer block dialog box shows that the block writes to a base workspace variable named gainParam.

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.

The Paramater Writer blocks writes to a model workspace variable named Param2.

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.

The Model block dialog box shows that the Paramater Writer block writes to model instance parameter named 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:

  1. In the Simulink Toolstrip, on the Debug tab, select Information Overlays > Connectors.

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

Yellow lines show the parameter connectors between the Parameter Writer and parameter owner 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

expand all

Parameter value written by the block.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus

Parameters

expand all

Since R2023a

Locally enable parameter validation for this block.

When parameter validation is disabled for a Paramer Writer block, normal mode simulation of the block is faster.

Tips

To globally enable or disable parameter validation for Parameter Writer blocks, use the Parameter Writer block validation configuration parameter.

Dependencies

To disable parameter validation for this block, the block must not directly or indirectly change the value of a Model block instance parameter. For example, the block must not change the value of variable or mask parameter that specifies the value of a Model block instance parameter.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: IsParameterValidationOn
Values: 'on' (default) | 'off'

Example: set_param(gcb,'IsParameterValidationOn','off')

Since R2024a

Write to block parameters, model workspace variables, base workspace variables, or variables you create in Simulink.data.Dictionary. Select one of these options:

  • Block parameter — Write to tunable parameters of built-in blocks such as Gain block, Constant block, 1-D Lookup Table block. To check whether supports a block parameter is supported by the Parameter Writer block, select the block use get_param(blk, 'IsParamOwnerBlock'), where blk is the block name or block handle. If the function returns 'on', the block parameter is supported by the Parameter Writer block, otherwise the parameter is not supported. For more information, see, Identifying Parameter Owner Blocks.

  • Model workspace variable — Write to variables in the model workspace such as MATLAB variables, Simulink.Parameter, or Simulink.VariantControl object. However, you cannot use this block to write to a model workspace variable which is marked as an argument.

  • Base workspace variable — Write to variables in the base workspace or variables you create in Simulink.data.Dictionary.

Note

  • This parameter replaces the Access model workspace parameter parameter that existed in versions prior to R2024a.

  • You can switch the active variant during simulation or execution of the generated code by using the Parameter Writer block placed inside a conditionally executed subsystem or in an Initialize Function block, a Reinitialize Function block, a Reset Function block. Set the Destination parameter based on the location of your variant control variable and select the Validate parameter. For more information see Variant Subsystem.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: Destination
Values: 'Block parameter' (default) | 'Model workspace variable' | 'Base workspace variable'

Example: set_param(gcb, Destination="Block parameter")

Specify the name of the workspace variable whose value this block writes.

Note

The Workspace variable name parameter replaces the existing Model workspace parameter name parameter. (since R2023b)

Dependencies

To enable this parameter, select the Model workspace variable or Base workspace variable from the Destination parameter options.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: WorkspaceVariableName
Values: '' (default) | character vector | string scalar

Example: set_param(gcb,'WorkspaceVariableName','var')

Specify the block path of the parameter owner block and the name of the parameter whose value this block writes.

The Parameter Owner Selector Tree provides the writable parameters in a hierarchical tree list. Expand the nodes on the path to the parameter owner block. Then, select the parameter under the parameter owner block.

Tips

If the name of a parameter owner block contains a slash (/), the slash appears as two slashes (//) in the Parameter Writer block parameters. This notation distinguishes the slash in the block name itself from slashes that separate names at different levels of the model hierarchy.

Dependencies

To enable this parameter, select the Block parameter option from the Destination parameter list.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

First, specify the block path of a parameter owner block using the ParameterOwnerBlock parameter.

Parameter: ParameterOwnerBlock
Values: '' (default) | character vector | string scalar
Data Types: char | string

Example: set_param(gcb,'ParameterOwnerBlock','mymodel/Gain1')

Then, specify the programmatic name of a block parameter of the parameter owner block using the ParameterName parameter.

Parameter: ParameterName
Values: '' (default) | character vector | string scalar

Example: set_param(gcb,'ParameterName','Gain')

Block Characteristics

Data Types

Boolean | bus | double | enumerated | fixed point | integer | single

Direct Feedthrough

no

Multidimensional Signals

yes

Variable-Size Signals

no

Zero-Crossing Detection

no

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

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced in R2016b

expand all