Documentation

Parameterize Instances of a Reusable Referenced Model

When you use model referencing to break a large system into components, each component is a separate model. You can reuse a component by referring to it with multiple Model blocks. Each Model block is an instance of the component. You can then configure a block parameter (such as the Gain parameter of a Gain block) to use either the same value or a different value for each instance of the component. To use different values, create and use a model argument to set the value of the block parameter.

For information about setting block parameter values in reusable components, see Parameter Interfaces for Reusable Components.

Specify Different Value for Each Instance of Reusable Model

To specify a different value for a block parameter in each instance of a reusable referenced model:

  1. Create a MATLAB® variable or Simulink.Parameter object in the referenced model workspace.

    Use a MATLAB variable for ease of maintenance. Use a Simulink.Parameter object for greater control over the minimum and maximum value, the data type, and other properties of the model argument.

  2. Set the block parameter value in the model by using the variable or object. Optionally, use the same variable or object to set other block parameter values.

    When you simulate this model directly, the block parameters use the value that the variable or object stores in the model workspace. Otherwise, including when you simulate a parent model, the value in the model workspace has no significance.

  3. Configure the variable or object as a model argument. In the Model Explorer Model Hierarchy pane, select the node that corresponds to the referenced model workspace. In the Dialog pane (the right pane), set the value of the Model arguments parameter by using the name of the variable or object.

    To configure multiple variables or objects in the model workspace as model arguments, set the Model arguments parameter value by using a list of the variable or object names. Separate the names by using commas, spaces, or semicolons.

  4. In each Model block that refers to the reusable model, specify an instance-specific value for the model argument. To set the value, you can use the common techniques for setting block parameter values (see Set Block Parameter Values).

    To specify values for multiple arguments in the same Model block, use a list separated by commas, spaces, or semicolons.

Combine Multiple Arguments into a Structure

When you configure a model to use multiple model arguments, consider using a structure instead of separate variables in the model workspace. This technique reduces the effort of maintenance when you want to add, rename, or delete arguments. Instead of manually synchronizing the list of arguments in the model workspace with the list of argument values in Model blocks, you modify structures by using the Variable Editor or the command prompt.

If you have Simulink® Coder™, this technique can also reduce the ROM consumed by the formal parameters of the referenced model functions, such as the output (step) function.

For basic information about creating and using structures to set block parameter values, see Organize Related Block Parameter Definitions in Structures.

Parameterize a Referenced Model

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

For an example that involves code generation, see Specify Instance-Specific Parameter Values for Reusable Referenced Model.

Configure Referenced Model to Use Model Arguments

Create the model ex_model_arg_ref. This model represents a reusable algorithm.

open_system('ex_model_arg_ref')

In the Model Explorer Model Hierarchy pane, expand the node ex_model_arg_ref and select Model Workspace.

Select Add > Simulink Parameter twice. Two Simulink.Parameter objects appear in the Contents pane.

Rename the objects as gainArg and coeffArg.

Set the Value property of the objects. For example, set them to 3.17 and 1.05, respectively.

modelWorkspace = get_param('ex_model_arg_ref','ModelWorkspace');
assignin(modelWorkspace,'gainArg',Simulink.Parameter(3.17));
assignin(modelWorkspace,'coeffArg',Simulink.Parameter(1.05));

In the Model Hierarchy pane, select Model Workspace again.

In the Dialog pane (the right-hand pane), set Model arguments to coeffArg,gainArg.

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

In the ex_model_arg_ref model, in the Gain block dialog box, set Gain to gainArg.

set_param('ex_model_arg_ref/Gain','Gain','gainArg')

In the Discrete Filter block dialog box, set Numerator to coeffArg.

set_param('ex_model_arg_ref/Discrete Filter','Numerator','coeffArg')

Save the ex_model_arg_ref model.

When you simulate the ex_model_arg_ref model by itself, the parameter objects in the model workspace use the values that you specified in the Value property. The block parameters also use these values.

Set Model Argument Values in Parent Model

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

open_system('ex_model_arg')

Right-click the upper Model block and select Block Parameters.

Set Model argument values by using two numbers separated by a comma. For example, use 0.98,2.98.

set_param('ex_model_arg/Model','ParameterArgumentValues','0.98,2.98')

In the other Model block dialog box, set Model argument values to two different numbers, for example 1.11,3.34.

set_param('ex_model_arg/Model1','ParameterArgumentValues','1.11,3.34')

When you simulate the ex_model_arg model, each instance of ex_model_arg_ref (each Model block) passes the argument values that you specified to the parameter objects in the ex_model_arg_ref model workspace. For example, in the upper instance of ex_model_arg_ref, the parameter object gainArg uses the value 2.98.

Group Multiple Model Arguments into Single Structure

At the command prompt, create a structure. Add one field for each of the parameter objects in the ex_model_arg_ref workspace.

structForInst1.gain = 3.17;
structForInst1.coeff = 1.05;

Store the structure in a Simulink.Parameter object.

structForInst1 = Simulink.Parameter(structForInst1);

Use the Model Explorer to copy the parameter object into the ex_model_arg_ref model workspace.

In the model workspace, rename structForInst1 as structArg.

assignin(modelWorkspace,'structArg',copy(structForInst1));

In the Model Hierarchy pane, select the ex_model_arg_ref model workspace. In the Dialog pane, set Model arguments to structArg.

set_param('ex_model_arg_ref','ParameterArgumentNames','structArg')

In the ex_model_arg_ref model, set the Gain parameter of the Gain block to structArg.gain.

set_param('ex_model_arg_ref/Gain','Gain','structArg.gain')

In the Discrete Filter block dialog box, set Numerator to structArg.coeff.

set_param('ex_model_arg_ref/Discrete Filter',...
    'Numerator','structArg.coeff')

Save the ex_model_arg_ref model.

At the command prompt, copy the existing structure as structForInst2.

structForInst2 = copy(structForInst1);

Set the field values in the two structures by using the same numbers that you used to set the model argument values in the Model blocks.

structForInst1.Value.gain = 2.98;
structForInst1.Value.coeff = 0.98;

structForInst2.Value.gain = 3.34;
structForInst2.Value.coeff = 1.11;

In the upper Model block dialog box, set Model argument values to structForInst1.

set_param('ex_model_arg/Model','ParameterArgumentValues','structForInst1')

In the other Model block dialog box, set Model argument values to structForInst2.

set_param('ex_model_arg/Model1','ParameterArgumentValues','structForInst2')

With the structures, the mathematical functionality of the models is the same.

Use Bus Object as Data Type of Structures

Optionally, use a Simulink.Bus object as the data type of the structures.

Use the function Simulink.Bus.createObject to create the bus object. The hierarchy of elements in the object matches the hierarchy of the structure fields. The default name of the object is slBus1.

Simulink.Bus.createObject(structForInst1.Value);

Rename the bus object by copying it.

myParamStructType = copy(slBus1);

Set the data type of the parameter objects in the base workspace by using the bus object.

structForInst1.DataType = 'Bus: myParamStructType';
structForInst2.DataType = 'Bus: myParamStructType';

Use the Model Explorer to view the contents of the ex_model_arg_ref model workspace.

Set Column View to Data Objects. For the parameter object structArg, set DataType to Bus: myParamStructType.

temp = getVariable(modelWorkspace,'structArg');
temp = copy(temp);
temp.DataType = 'Bus: myParamStructType';
assignin(modelWorkspace,'structArg',copy(temp));

Rename Model Argument

To rename a model argument in the context of the referenced model, in the Model Explorer, right-click the variable or object in the referenced model workspace and select Rename All. The renaming operation changes the name of the variable or object and changes references to it throughout the model For more information, see Manage Variables from Block Parameter.

Customize User Interface for Reusable Component

When you design a reusable referenced model for use by other members of a team, you can apply a mask to a Model block that references the model. You can then customize the way that your users set model argument values.

  1. Configure the referenced model to use model arguments.

  2. Mask a Model block that references the model. Add this block to a custom block library.

  3. Add mask parameters to the mask. Add one mask parameter per model argument.

  4. Use the mask parameters to set the argument values in the Model block.

Your users can specify the argument values for each instance of this block by using the custom mask parameters that you define.

Using this technique also makes it easier to programmatically specify argument values. If you create and use a mask parameter named gainMask, to programmatically set the argument value to 0.98 for an instance of the block named myModelBlock, your users can use this command at the command prompt:

set_param('myModelBlock','gainMask','0.98')

If you do not mask the block, to set the argument value, your users must locate the value in the list of argument values in the Model block.

For information about masking blocks, see Masking Fundamentals. For information about block libraries, see Create a Custom Library. For an example model, see Detailed Workflow for Managing Data with Model Reference.

Configure Instance-Specific Data for Lookup Tables

When you use Simulink.LookupTable objects to store and configure lookup table data for ASAP2 or AUTOSAR code generation (for example, STD_AXIS or CURVE), you can configure the objects as model arguments. You can then specify unique table data and breakpoint data for each instance of a component.

  1. In the referenced model workspace, create Simulink.LookupTable objects. In lookup table blocks that are in the model, use the objects to set the table and breakpoint data.

  2. Configure the objects as model arguments by using the Model arguments parameter in the Model Explorer.

  3. Create Simulink.LookupTable objects to store the instance-specific data. For example, if you configure a single Simulink.LookupTable object in the referenced model workspace as a model argument, to store instance-specific data for two instances of the referenced model (Model blocks in the parent model), create two Simulink.LookupTable objects in the base workspace.

  4. Configure all of the Simulink.LookupTable objects to use the same structure type name. In the property dialog box, under Struct Type definition, set Name to the same value.

  5. Use the instance-specific objects to set argument values in Model blocks. For example, in each Model block, use one of the instance-specific Simulink.LookupTable objects from the base workspace.

You cannot use Simulink.Breakpoint objects or Simulink.LookupTable objects that refer to Simulink.Breakpoint objects as model arguments.

Create Example Models

Create the example model ex_arg_LUT_ref, which represents a reusable algorithm.

open_system('ex_arg_LUT_ref')

Create the example model ex_arg_LUT, which uses the reusable algorithm twice.

open_system('ex_arg_LUT')

Configure Model Arguments in Referenced Model

Use the Model Explorer to create a Simulink.LookupTable object in the referenced model workspace (ex_arg_LUT_ref). Name the object LUT_arg.

In the Dialog pane (the right pane) of the Model Explorer, set Number of table dimensions to 2. Under Table and Breakpoints, specify values for the table and breakpoint data. When you simulate or generate code directly from ex_arg_LUT_ref, the model uses these values.

Under Struct Type definition, set Name to LUT_arg_Type.

Click Apply.

In the Model Hierarchy pane, select the node that corresponds to the referenced model workspace.

In the Dialog pane, set Model arguments to LUT_arg. Click Apply.

In the referenced model, in the n-D Lookup Table block, set Data specification to Lookup table object. Set Name to LUT_arg.

Save the referenced model.

At the command prompt, you can use these commands to create and configure the object.

temp = Simulink.LookupTable;
temp.Table.Value = [3 4; 1 2];
temp.Breakpoints(1).Value = [1 2];
temp.Breakpoints(2).Value = [3 4];
temp.StructTypeInfo.Name = 'LUT_arg_Type';
mdlwks = get_param('ex_arg_LUT_ref','ModelWorkspace');
assignin(mdlwks,'LUT_arg',copy(temp))
set_param('ex_arg_LUT_ref','ParameterArgumentNames','LUT_arg')
set_param('ex_arg_LUT_ref/n-D Lookup Table',...
    'DataSpecification','Lookup table object','LookupTableObject','LUT_arg')
save_system('ex_arg_LUT_ref')

Create Instance-Specific Argument Values

At the command prompt, create two Simulink.LookupTable objects in the base workspace.

LUTForInst1 = Simulink.LookupTable;
LUTForInst2 = Simulink.LookupTable;

Specify breakpoint and table data for each object.

LUTForInst1.Table.Value = [8 7; 6 5];
LUTForInst1.Breakpoints(1).Value = [5 6];
LUTForInst1.Breakpoints(2).Value = [3 4];

LUTForInst2.Table.Value = [9 8; 7 7];
LUTForInst2.Breakpoints(1).Value = [3 4];
LUTForInst2.Breakpoints(2).Value = [5 6];

Specify a structure type name. Match this name with the name specified by the object in the referenced model workspace.

LUTForInst1.StructTypeInfo.Name = 'LUT_arg_Type';
LUTForInst2.StructTypeInfo.Name = 'LUT_arg_Type';

In the ex_arg_LUT model, in the top Model block, set Model argument values to LUTForInst1.

In the bottom Model block, set Model argument values to LUTForInst2.

At the command prompt, you can use these commands to configure the Model blocks.

set_param('ex_arg_LUT/Model','ParameterArgumentValues','LUTForInst1')
set_param('ex_arg_LUT/Model1','ParameterArgumentValues','LUTForInst2')

Each instance of ex_arg_LUT_ref uses the table and breakpoint data stored in one of the Simulink.LookupTable objects in the base workspace.

See Also

| |

Related Examples

Was this topic helpful?