Documentation |
On this page… |
---|
A parameterized referenced model obtains values that affect the behavior of the referenced model from some source outside the model. Changing the values changes the behavior of the model, without recompiling the model.
Due to the constraints described in Inline Parameter Requirements, you cannot use the Model Parameter Configuration dialog box to parameterize referenced models. Simulink^{®} provides three other techniques that you can use to parameterize referenced models:
Global nontunable parameters
Global tunable parameters
Model arguments
Global parameters work the same way with referenced models that they do with any other model construct. Each global parameter has the same value in every instance of a referenced model that uses it. Model arguments allow you to provide different values to each instance of a referenced model. Each instance can then behave differently from the others. The effect is analogous to calling a function more than once with different arguments in each call.
You can use a structure parameter to group variables for parameterizing model references. For details, see Structure Parameters.
A global nontunable parameter is a MATLAB^{®} variable or a Simulink.Parameter object whose storage class is auto. The parameter must exist in the MATLAB workspace.
Using a global nontunable parameter in a referenced model sets the parameter value before the simulation begins. This allows you to control the behavior of the referenced model. All instances of the model use the same value. You cannot change the value during simulation, but you can change it between one simulation and the next. The change requires rebuilding the model in which the change occurs, but not any models that it references. See Specify Parameter Values for details.
A global tunable parameter is a Simulink.Parameter object whose storage class is other than auto. The parameter exists in the MATLAB workspace.
Using a global tunable parameter in a referenced model allows you to control the behavior of the referenced model by setting the parameter value. All instances of the model use the same value. You can change the value during simulation or between one simulation and the next. The change does not require rebuilding the model in which the change occurs, or any models that it references. See Tunable Parameters for details.
To reference a model that uses tunable parameters defined with the Model Parameter Configuration Dialog Box, change the model to implement tunability another way. To facilitate this task, Simulink provides a command that converts tunable parameters specified in the Model Parameter Configuration dialog box to global tunable parameters. See tunablevars2parameterobjects for details.
Model arguments let you parameterize references to the same model so that each instance of the model behaves differently. Without model arguments, a variable in a referenced model has the same value in every instance of the model. Declaring a variable to be a model argument allows each instance of the model to use a different value for that variable.
To create model arguments for a referenced model:
Create MATLAB variables in the model workspace.
Add the variables to a list of model arguments associated with the model.
Specify values for those variables separately in each Model block that references the model
The values specified in the Model block replace the values of the MATLAB variables for that instance of the model.
A referenced model that uses model arguments can also appear as a top model or a standalone model. No Model block exists to provide model argument values. The model uses the values of the MATLAB variables as defined in the model workspace. You can use the same model, without changing it, as a top model, a standalone model, and a parameterized referenced model.
The sldemo_mdlref_datamngtsldemo_mdlref_datamngt model demonstrates techniques for using model arguments. The model passes model argument values to referenced models through masked Model blocks. Such masking can be convenient, but is independent of the definition and use of model arguments themselves. See Masking for information about masking.
The rest of this section describes techniques for declaring and using model arguments to parameterize a referenced model independently of any Model block masking. The steps are:
Create MATLAB variables in the model workspace.
Register the variables to be model arguments.
Assign values to those arguments in Model blocks.
To create MATLAB variables for use as model arguments:
Open the model for which you want to define model arguments.
In the Model Explorer Model Hierarchy pane, select the workspace of the model:
From Model Explorer's Add menu, select MATLAB Variable.
A new MATLAB variable appears in the Contents pane with a default name and value.
In the Contents pane:
Change the default name of the new MATLAB variable to a name that you want to declare as a model argument.
If you also use the model as a top or standalone model, specify the value for that variable for use in that context. This value must be numeric.
If the variable type does not match the dimensions and complexity of the model argument, specify a value that has the correct type. This type must be numeric.
Repeat adding and naming MATLAB variables until you have defined all the variables that you need.
To register MATLAB variables as model arguments:
Again, in the Model Explorer Model Hierarchy pane, select the workspace of the model.
The Dialog pane displays the Model Workspace dialog.
In the Model Workspace dialog, enter the names of the MATLAB variables that you want to declare as model arguments. Use a comma-separated list in the Model arguments field.
For example, suppose you added two MATLAB variables named init_value and incr, and you declared them to be model arguments. The Contents and Dialog panes of the Model Explorer could look like this:
If a model declares model arguments, assign values to those arguments in each Model block that references the model. Failing to assign a value to a model argument causes an error: the value of the model argument does not default to the value of the corresponding MATLAB variable. That value is available only to a standalone or top model. To assign values to the arguments of a referenced model:
Open the block parameters dialog box of the Model block by right-clicking the block and choosing Block Parameters (ModelReference) from the context menu.
The second field, Model arguments, specifies the same MATLAB variables, in the same order, that you previously typed into the Model arguments field of the Model Workspace dialog. This field is not editable. It provides a reminder of which model arguments need values assigned, and in what order.
In the Model argument values field, enter a comma-delimited list of values for the model arguments that appear in the Model arguments field. Simulink assigns the values to arguments in positional order, so they must appear in the same order as the corresponding arguments.
You can enter the values as literal values, variable names, MATLAB expressions, and Simulink parameter objects. Any symbols used resolve to values as described in Symbol Resolution Process. All values must be numeric (including objects with numeric values).
Note: Simulink generates an error if you use a model argument in an expression that is nontunable because of Tunable Expression Limitations. |
The value for each argument must have the same dimensions and complexity as the MATLAB variable that defines the model argument in the model workspace. The data types need not match. If necessary, the Simulink software casts a model argument value to the data type of the corresponding variable.
Click OK or Apply to confirm the values for the Model block.
When the model executes in the context of that Model block, the Model arguments have the values specified in the Model argument values field of the Model block.