Main Content

Parameterize a Referenced Model Programmatically

This example shows how to programmatically configure multiple instances of a referenced model to use different values for the same block parameter.

Configure Referenced Model to Use Model Arguments

When you simulate a model, the parameter objects in the model workspace use the values that you specify for the Simulink.Parameter objects, Simulink.LookupTable objects, or MATLAB® variables. The block parameters also use these values.

To configure the Gain parameter of a Gain block and the Numerator parameter of a Discrete Filter block as model arguments, follow these steps.

Open model ex_model_arg_ref. This model represents a reusable algorithm.

open_system('ex_model_arg_ref')

For the Gain block, set the value of the Gain parameter to a Simulink.Parameter object in the model workspace with a numeric value. For this example, name the Simulink.Parameter object gainArg and assign a value of 3.17.

set_param('ex_model_arg_ref/Gain','Gain','gainArg')
modelWorkspace = get_param('ex_model_arg_ref','ModelWorkspace');
assignin(modelWorkspace,'gainArg',Simulink.Parameter(3.17));

For the Discrete Filter block, set the value of the Numerator parameter to a Simulink.Parameter object in the model workspace with a numeric value. For this example, name the Simulink.Parameter object coeffArg and assign a value of 1.05.

set_param('ex_model_arg_ref/Discrete Filter','Numerator','coeffArg')
assignin(modelWorkspace,'coeffArg',Simulink.Parameter(1.05));

Specify gainArg and coeffArg as model arguments.

set_param('ex_model_arg_ref','ParameterArgumentNames','coeffArg,gainArg')

Set Model Argument Values in Parent Model

When you simulate a parent model, each instance of a reusable referenced model uses the argument values that you specify in the parent model. In this example, in the upper instance of ex_model_arg_ref, the parameter object gainArg uses the value 2.98.

Model ex_model_arg contains two Model blocks that reference ex_model_arg_ref. To set different parameter values for the two instances of the model, follow these steps.

Open model ex_model_arg. This model represents a system model that uses multiple instances of the reusable algorithm.

open_system('ex_model_arg')

For both instances of model ex_model_arg, set values for the model arguments. If you decide to re-promote these arguments, set the Argument field to true. By default, the Argument field is false.

instSpecParams = get_param('ex_model_arg/Model','InstanceParameters');
instSpecParams1 = get_param('ex_model_arg/Model1','InstanceParameters');

instSpecParams(1).Value = '.98';
instSpecParams(2).Value = '2.98';
instSpecParams1(1).Value = '1.11';
instSpecParams1(2).Value = '3.34';
instSpecParams(1).Argument = true;
instSpecParams(2).Argument = true;
instSpecParams1(1).Argument = true;
instSpecParams1(2).Argument = true;

set_param('ex_model_arg/Model','InstanceParameters',instSpecParams);
set_param('ex_model_arg/Model1','InstanceParameters',instSpecParams1);

Related Topics