Main Content

set_param

Set Simulink parameter value

Description

set_param(object,parameter1,value1,...,parameterN,valueN) sets the specified Simulink® parameter parameter to the specified value value for the target object specified by object. The target object can be a model, subsystem, library, block, line, port, or bus element port element.

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 efficient because one call evaluates the parameters only once. If any parameter names or values are invalid, then the function does not set any parameters.

example

Examples

collapse all

You can set the value of a block parameter using the set_param function with these input arguments:

  • Block handle or full block path including block name, for example, 'myModel/mySubsystem/myblock'

  • Parameter name

  • New value you want to assign to the parameter

For more information about block handles and paths, see Get Handles and Paths. For an example of how to get the block path, see Get Block Paths and Names.

For information about how to get the parameter names, see Get Block Parameter Names.

In this example, you set the coefficients of the transfer function specified by a Transfer Function block named Alpha-sensor Low-pass Filter.

  • The block is located in the Controller subsystem of the slexAircraftExample model.

  • The coefficients of the transfer function numerator are stored in the Numerator parameter. The coefficients of the transfer function denominator are stored in the Denominator parameter.

  • The new value you want to assign to the numerator is 1.2. The new value you want to assign to the denominator is [0.8*Tal,1], where Tal is a variable.

Open the example. Then, load the slexAircraftExample model.

mdl = 'slexAircraftExample';
load_system(mdl)

Set the transfer function coefficients.

path = [mdl,'/Controller/Alpha-sensor Low-pass Filter'];
set_param(path,'Numerator','1.2','Denominator','[0.8*Tal,1]');

To check the values of the Numerator and Denominator parameters, use the get_param function.

num = get_param(path,'Numerator')
num = 
'1.2'
denom = get_param(path,'Denominator')
denom = 
'[0.8*Tal,1]'

When the block path is long and you want to set multiple parameters, consider using block handles.

You can set the value of a block parameter using the set_param function with these input arguments:

  • Block handle or full block path including block name, for example, 'myModel/mySubsystem/myblock'

  • Parameter name

  • New value you want to assign to the parameter

For information about how to get the block path, see Get Block Paths and Names.

For information about how to get the parameter names, see Get Block Parameter Names.

In this example, you set the coefficients of the transfer function specified by a Transfer Function block named Alpha-sensor Low-pass Filter.

  • The block is located in the Controller subsystem of the slexAircraftExample model.

  • The coefficients of the transfer function numerator are stored in the Numerator parameter. The coefficients of the transfer function denominator are stored in the Denominator parameter.

  • The new value you want to assign to the numerator is 1.2. The new value you want to assign to the denominator is [0.8*Tal,1], where Tal is a variable.

Open the example. Then, get the block handle using the getSimulinkBlockHandle function. To simultaneously get the block handle and load the model, specify the second argument for the getSimulinkBlockHandle function as true.

path = 'slexAircraftExample/Controller/Alpha-sensor Low-pass Filter';
h = getSimulinkBlockHandle(path,true)
h = 
459.0009

To set the parameter values, specify the block using the handle. The handle contains a double. Do not try to manually enter the number of a handle, because you may need to specify more digits than MATLAB® displays. Instead, assign the handle to a variable, and use the variable name to specify the block.

set_param(h,'Numerator','1.2','Denominator','[0.8*Tal,1]');

You can set the value of a model parameter using the set_param function with these input arguments:

  • Model name

  • Parameter name

  • New value you want to assign to the parameter

For information about how to get model parameter names, see Get Model Parameter Names.

Open the example. Then, load the f14 model.

load_system('f14')

Set the solver of the f14 model to ode15s and the stop time to 3000 seconds.

set_param('f14','Solver','ode15s','StopTime','3000')

To check the values of the Solver and StopTime parameters, use the get_param function.

solver = get_param('f14','Solver')
solver = 
'ode15s'
stoptime = get_param('f14','StopTime')
stoptime = 
'3000'

Open the example. Then, open the f14 model.

open_system('f14')

Get the handle of the Signal Generator block named Pilot.

h = getSimulinkBlockHandle('f14/Pilot');

Get the position of the block.

pos = get_param(h,'Position')
pos = 1×4

    15    18    55    42

The function returns the block position, specified as a vector of coordinates measured in pixels: [left top right bottom].

Move the block to the right.

pos = [pos(1)+20 pos(2) pos(3)+20 pos(4)];
set_param(h,'Position',pos);

Move the block up.

pos = [pos(1) pos(2)-20 pos(3) pos(4)-20];
set_param(h,'Position',pos);

Stretch the block horizontally.

pos = [pos(1)/4 pos(2) pos(3) pos(4)];
set_param(h,'Position',pos);

Open the example. Then, open the f14 model.

open_system('f14')

The model has a Signal Generator block named Pilot. Move the block name to the top of the block.

set_param('f14/Pilot','NameLocation','top')

Input Arguments

collapse all

Name, path, or handle of object, specified as a character vector, string scalar, or numeric scalar.

How you specify the target object depends on its type.

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

Example: 'vdp/Mu'

Example: 'mymodel/Subsystem1/Out1.nonsinusoidal.saw'

Tips

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

  • Do not try to manually specify the number of a handle, for example, 5.007, because you usually need to specify more digits than MATLAB® displays. Assign the handle to a variable and use that variable name.

Data Types: char | string | double

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

For information about parameters, properties, or attributes, see the programmatic use information on the corresponding reference pages. For example:

To get all parameters of a target object, use the get_param function with the 'ObjectParameters' option. Some of the returned parameters may be for internal use only.

Example: 'Solver'

Example: 'SimulationCommand'

Example: 'Position'

Example: 'NameLocation'

Data Types: char | string

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

Many block parameter values are specified as character vectors or string scalars. 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 /.

Tips

After you set parameters in the MATLAB workspace, to see the changes in a model, update the diagram.

set_param(model,'SimulationCommand','Update')

Version History

Introduced before R2006a