Main Content

setparam

Change value of tunable parameter in real-time application

Since R2020b

Description

setparam(target_object, block_path, parameter_name, parameter_value,'Force',true) sets the value of a tunable block parameter to a new value. Specify the block parameter by the block name and the parameter name.

example

setparam(target_object, '', parameter_name, parameter_value,'Force',true) sets the value of the tunable global parameter to a new value. Specify the global parameter by the MATLAB® variable name.

example

Examples

collapse all

Set the value of the block parameter 'Amplitude' of the block 'Signal Generator' to 5.

tg = slrealtime;
mdlSTF = getSTFName(tg);
mdlName = 'slrt_ex_osc';
openExample(mdlName);
set_param(mdlName,"SystemTargetFile",mdlSTF);
xfername = [mdlName,'/Signal Generator'];
slbuild(mdlName);
load(tg,mdlName);
setparam(tg,xfername,'Amplitude',5)

Sweep the value of the block parameter 'Amplitude' of the block 'Signal Generator' by steps of 2.

tg = slrealtime;
mdlSTF = getSTFName(tg);
mdlName = 'slrt_ex_osc';
openExample(mdlName);
set_param(mdlName,"SystemTargetFile",mdlSTF);
xfername = [mdlName,'/Signal Generator'];
slbuild(mdlName);
load(tg,mdlName);
for i = 1 : 3
    setparam(tg,xfername,'Amplitude',(i*2))
end

Set the value of the MATLAB variable 'Freq' to 30.

tg = slrealtime;
mdlSTF = getSTFName(tg);
mdlName = 'slrt_ex_osc';
openExample(mdlName);
set_param(mdlName,"SystemTargetFile",mdlSTF);
Freq = Simulink.Parameter;
Freq.StorageClass = 'ExportedGlobal';
Freq.Value = 10;
xfername = [mdlName,'/Signal Generator'];
set_param(xfername,'Frequency','Freq');
slbuild(mdlName);
load(tg,mdlName);
setparam(tg,'','Freq',30)

Set the value of the MATLAB variable 'oscp.G2' to 10000000.

tg = slrealtime;
mdlSTF = getSTFName(tg);
mdlName = 'slrt_ex_osc_struct';
openExample(mdlName);
set_param(mdlName,"SystemTargetFile",mdlSTF);
load('slrt_ex_osc_struct.mat');
slbuild(mdlName);
load(tg,mdlName);
setparam(tg,'','spkp.g2_gain',10000000)

The getparam and setparam functions support dot notation syntax to access parameter values in real-time applications. These are examples of more advanced syntax.

% If a parameter is a struct, a single element of any 
% array can be specified at any arbitrary depth in the struct.
tg.setparam('', 'p.a.b(2).c', val) 
val = tg.getparam('', 'p.a.b(2).c') 

% If a parameter is an array of structs, one element of 
% the struct array can be specified as follows:
tg.setparam('', 'p(2,2).x.y.z', val) 
val = tg.getparam('', 'p(2,2).x.y.z') 

% If a parameter is N dimensions, a single element of 
% the parameter can be accessed by specifying each dimension.
tg.setparam('top/constant', 'Value(3,4)', val) 
val = tg.getparam('top/constant', 'Value(3,4)') 

% If a parameter is Mx1 or 1xN (row or column vector), 
% the following syntax specifying a single index 
% is allowed: 
tg.setparam('top/constant1', 'Value(4)', val)

Simulink® Real-Time™ cannot tune block parameters in referenced models. You can tune workspace parameters, model arguments, and model instance parameters in referenced models. This example shows how to parameterize a referenced model and use setparam to access an instance variable. For more information, see Configure Instance-Specific Values for Block Parameters in a Referenced Model.

  1. Open example SlrtConcurrentExecutionExample. In the MATLAB command window, type:

    openExample('slrealtime/SlrtConcurrentExecutionExample');
  2. Open sub model slrt_ex_mds_subsystem2.

    openExample('slrt_ex_mds_subsystem2')
  3. Open the Model Explorer and select the Model Workspace for the slrt_ex_mds_subsystem2 model.

  4. Select Add > MATLAB Variable then change the variable name from Var to VarGain1. Set the variable value to 0.05 and set the Argument checkbox to On.

  5. Apply the variable change, closed the Model Explorer, and save and close the slrt_ex_mds_subsystem2 model.

  6. Open top model slrt_ex_mds_and_tasks, build, and load the real-time application. Before building, if you have not previously done so, configure the models for your target computer by using the Speedgoat Target Platform Selector utility.

  7. Access the VarGain1 variable by using setparam.

    setparam(tg,{'slrt_ex_mds_and_tasks/Model2'}, 'VarGain1',0.03)
  8. View the new value for VarGain1 by using getparam.

    getparam(tg,{'slrt_ex_mds_and_tasks/Model2'}, 'VarGain1')
    
    ans =
    
        0.0300
  9. You also can use this approach for Simulink instance parameters. The only change to the procedure is adding a Simulink Parameter instead of adding a MATLAB variable. By using the cell array syntax, you can access workspace parameters, model arguments, and model instance parameters in any level of referenced models. For example, to access a Simulink instance parameter Kinst in a deeply layered reference model:

    mModelRefParams_top/T (mModelRefParams_T)/M1 (mModelRefParams_M)/S1 (mModelRefParams_S)

    You could use this syntax:

    setparam(tg,{'mModelRefParams_top/T', 'mModelRefParams_T/M1', 'mModelRefParams_M/S1'}, 'Kinst', 0.5)

Input Arguments

collapse all

Provides access to methods that manipulate the target computer properties.

Example: tg

The block_path values can be:

  • Empty character vector ('') or empty string scalar ("") for base or model workspace variables

  • Character vector or string scalar for block path to parameters in the top model

  • Cell array of character vectors or string scalars for model block arguments and model instance parameters

Example: '', 'Gain1', {'top/model','sub/model'}

The parameter can designate either a block parameter or a global parameter that provides the value for a block parameter. The block parameter or MATLAB variable must be observable to be accessible through the parameter name.

Note

Simulink Real-Time does not support parameters of multiword data types.

Example: 'Gain', 'oscp.G1', 'oscp', 'G2'

New value with data type as required by parameter.

Example: 1

The optional 'Force', true name-value pair argument forces the parameter value set operation even if the value is not in the range of [min max] for the parameter.

Example: 'Force', true

Data Types: logical

Version History

Introduced in R2020b

expand all