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 theSimulink.Parameter
objects,Simulink.LookupTable
对象,或MATLAB®变量。块参数s also use these values.
To configure theGainparameter of a Gain block and theNumeratorparameter of a Discrete Filter block as model arguments, follow these steps.
Open modelex_model_arg_ref
. This model represents a reusable algorithm.
open_system('ex_model_arg_ref')
For the Gain block, set the value of theGain参数aSimulink.Parameter
object in the model workspace with a numeric value. For this example, name theSimulink.Parameter
objectgainArg
and assign a value of3.17
.
set_param('ex_model_arg_ref/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 theNumerator参数aSimulink.Parameter
object in the model workspace with a numeric value. For this example, name theSimulink.Parameter
objectcoeffArg
and assign a value of1.05
.
set_param('ex_model_arg_ref/Discrete Filter','Numerator','coeffArg') assignin(modelWorkspace,'coeffArg',Simulink.Parameter(1.05));
SpecifygainArg
andcoeffArg
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 ofex_model_arg_ref
, the parameter objectgainArg
uses the value2.98
.
Modelex_model_arg
contains two Model blocks that referenceex_model_arg_ref
. To set different parameter values for the two instances of the model, follow these steps.
Open modelex_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 modelex_model_arg
, set values for the model arguments. If you decide to re-promote these arguments, set theArgument
field totrue
. By default, theArgument
field isfalse
.
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);