Main Content

set_param

R2026b

Set Simulink parameter value

Description

set_param(object,parameter1=value1,...,parameterN=valueN) sets the specified parameter value for the target object. For example, you can use this syntax to change the amplitude value of a Sine Wave block. The target object can be a model, subsystem, library, block, line, port, or bus element port element specified as a handle or path. For information on how to get handles and paths, see Get Handles and Paths.

To set multiple parameter values for the target object, call the set_param function once with multiple name-value arguments instead of separately calling the function for each parameter. Setting multiple parameters with one function call is faster because one call evaluates the parameters only once. If any parameter names or values are invalid, then the function does not set any parameters.

To programmatically interact with a simulation, use the set_param function with the SimulationCommand name-value argument. For more information, on the page Run Simulations Programmatically, see the section Interact with Simulations by Issuing Simulation Commands.

For most parameters, running set_param regardless of whether the specified value is different from the current value is faster than comparing the values and only running the function if the values differ.

Changing a parameter value using the set_param function dirties the model, even when the parameter only changes a mode. For example, changing the variant control mode by setting the value of the LabelModeActiveChoice parameter dirties the model.

example

Examples

collapse all

Suppose you have a loaded model named myModel that contains a Subsystem block named mySubsystem. The subsystem contains a Gain block named myBlock. Set the gain value to 5.

To set the value, you need three inputs:

In this example, you have enough information to get the block path. The block path is the path through the model hierarchy from the root of the model to the block, with each step through the hierarchy separated by a forward slash. Store the block path in a variable.

blockPath = "myModel/mySubsystem/myBlock";

Get the programmatic name of the parameter whose value you want to set. One way to do so is to check the documentation for the block. On the documentation page for the block, in the Parameters section, find the parameter. Then, check the Programmatic Use section for the programmatic name.

For this example, check this page for the parameter name: Gain. The programmatic name of the parameter storing the gain value is Gain.

Check the Programmatic Use section of the parameter documentation for the permissible value types. In this case, the new value must be expressed as a string or character vector.

Set the parameter value.

gainValue = get_param(blockPath,Gain="5");

If you want to run more operations on the block, consider using its handle instead of the block path. To get the handle from the block path, use this command.

h = getSimulinkBlockHandle(blockPath);

Set the parameter value using the handle.

gainValue = set_param(h,Gain="5");

Suppose you have a loaded model with a complex hierarchy containing hundreds of blocks. The handle of the model is stored in the variable h. Set the transfer function numerator of all Transfer Fcn blocks whose name contains the key phrase Low-Pass Filter to 1.

To set the value, you need three inputs:

Get the paths of the Transfer Fcn blocks whose name contains the key phrase Low-Pass Filter. Use the find_system function.

blockPaths = find_system(h,Regexp="on",CaseSensitive="on",Type="Block",BlockType="TransferFcn",Name=".*Low-Pass Filter.*")

Get the programmatic name of the parameter whose value you want to set. One way to do so is to check the documentation for the block. On the documentation page for the block, in the Parameters section, find the parameter. Then, check the Programmatic Use section for the programmatic name.

For this example, check this page for the parameter name: Transfer Fcn. The programmatic name of the parameter storing the numerator coefficient of the transfer function is Numerator.

Check the Programmatic Use section of the parameter documentation for the permissible value types. In this case, the new value must be expressed as a string or character vector.

Set the transfer function numerator.

set_param(blockPaths,Numerator="1");

Suppose you have a loaded model named myModel. Set the solver of the model to ode15s and the stop time to 3000 seconds.

To set the values, you need three inputs:

In this example, you already have the model name and the new values.

Get the programmatic name of the parameter you want to set. One way to do so is to check the documentation. Open this page: Set Model Configuration Parameters for a Model. Navigate to the Configuration Panes section. In the Configuration Panes section, click the link to the documentation page about the pane in the Configuration Parameters dialog box that lets you set the parameters of interest. On the page that opens, click the links to the documentation pages about the parameters of interest. Then, check the Programmatic Use section for the programmatic names.

For this example, you are looking for the solver and stop time parameters. You can set the solver and stop time of the model on the Solver pane of the Configuration Parameters dialog box. In the Configuration Panes section of the documentation, click the Solver Pane link. On the page that opens, click the Solver and Stop Time links and check the Programmatic Use section of each. The programmatic names of the parameters are Solver and StopTime

Check the Programmatic Use section of the parameter documentation for the permissible value types. In this case, both new values must be expressed as a string or character vector.

Set the solver and stop time.

set_param("myModel",Solver="ode15s",StopTime="3000")

Input Arguments

collapse all

Handle, name, or path of target object, or root, to query. How you can specify the target object depends on the type of target object.

  • Model — Model name or handle.

  • Subsystem — Subsystem name or handle.

  • Library — Library name or handle.

  • Block — Block path or handle.

  • Line — Line handle.

  • Port — Port handle.

  • Port element — Path composed of the model name or subsystem block path, a forward slash, and the port name or bus element path. For a bus element port, the bus element path provides the hierarchy from the top-level bus to the target element, separating each name in the hierarchy with a dot.

For information about how to get handles and paths, see Get Handles and Paths. Format handles as numeric scalars or numeric vectors. Format names and paths as strings, string arrays, character vectors, or cell arrays of character vectors. To specify multiple objects with a common parameter, use a cell array of character vectors, a string array, or an array of handles. All the specified objects must have the specified parameter.

Example: "myModel/mySubsystem/myBlock"

Tips

  • If you make multiple calls to get_param for the same block, specify the block using a numeric handle. This method is more efficient than using the full block path with get_param.

  • When you output the value of a handle, the MATLAB® Command Window displays a number. Do not try to use the displayed number, because the Command Window does not display all the digits and handles do not persist across Simulink® sessions. Instead, store the handle value in a variable, and then use the variable to specify the target object.

Data Types: double | string | character vector

Parameter, property, or attribute name, specified as a string or character vector. Some names are case sensitive.

For information about parameters, properties, or attributes, see the Programmatic Use information on the corresponding documentation pages. For example:

To get all parameters of a target object, use the get_param function with the 'ObjectParameters' option.

Example: "SolverName"

Example: "SimulationCommand"

Example: "Position"

Example: "NameLocation"

Data Types: string | character vector

Parameter value, specified in the format determined by the parameter type. Some parameter values are case sensitive. Values are often strings or character vectors, but they can also be numeric scalars, arrays, and other types.

Many block parameter values are specified as strings or character vectors. Two exceptions are Position, which is specified as a vector, and UserData, which can be any data type.

Limitations

  • If you use matlab -nodisplay to start a session, you cannot use set_param to run a simulation. The -nodisplay mode does not support simulation using set_param. Use the sim function instead.

  • When you use the set_param function to rename a block or signal, these names are not valid: empty names, names that contain only newline characters, and names that begin or end with /.

Version History

Introduced before R2006a