NOTE: This example uses the same model as the more detailed example. As a result, the model uses certain features that this introductory example does not describe.

Example Requirements

During this example, Simulink® generates code in the code generation folder created in the current directory. If you do not want to (or if you cannot) generate files in this directory, you should change your working directory.

If you plan to alter the example models:

1. Preserve the example in its original state by copying the following files from your MATLAB® installation directory, without changing their names, to a different directory:

Increments the counter by a specified amount if the second input changes

Saturates the counter between the specified upper and lower limits

The referenced model outputs a bus signal that contains:

Count: the value of the counter as an 8-bit integer

OverflowState: an enumerated value that indicates whether the counter is at the upper limit, lower limit, or in range

Example Outline

The sequence of steps in the example is as follows:

1. Prepare the referenced model (sldemo_mdlref_counter_datamngt) to use structures for the signals and parameters in its external interface.

2. Prepare the top model (sldemo_mdlref_datamngt) to call the referenced model.

3. Simulate the top model and examine the results.

Referenced Model: Setting Up Parameters

The referenced model has two model arguments (CounterParams, CounterICs) that parameterize the blocks in the model. Model arguments provide different parameter values to each instance of a referenced model. In this model, the arguments are defined as parameter structures to reduce the number of arguments being passed to the referenced model.

NOTE: This example uses an 8-bit integer for the counter, so the numeric fields also use 8-bit integers.

3. Use the MATLAB structures as model arguments.

Model arguments are defined as variables in the model workspace of a referenced model. You can use the Model Explorer to view and edit the contents of the model workspace.

You can initialize the model workspace from a number of different data sources. This example uses a MATLAB script (sldemo_mdlref_counter_datamngt_wsdata) to create the parameter structures to define the model arguments. Using a MATLAB script makes it easy to create and modify the parameter structure outside the model. It also facilitates incremental changes, version control, and data differencing.

Set the workspace's Data source to create the MATLAB structure in the model workspace and enter the variable name (CounterParams) into the Model arguments field in the model workspace's dialog box.

Referenced Model: Defining the Shape of the Output Bus Signal

The referenced model produces two results and packages them into a bus signal:

Count: the value of the counter as an 8-bit integer

OverflowState: an enumerated value that indicates whether the counter is at the upper limit, lower limit, or in range

2. Configure the root outport of the referenced model to output a nonvirtual bus signal based on this bus object (OutputType).

Referenced Model: Setting Initial Value for Bus Output

In general, the initial values for bus signals and states can be specified as '0', in which case all of the elements of the bus will be initialized to zero (or the relevant ground value). However, in certain cases, it is desirable to specify nonzero initial values for bus signals and states. In this example, the initial condition of the counter is tunable, so the initial value of output signal must be set consistently.

To specify the initial value of the output of the referenced model:

1. Create a parameter structure that is compatible with the bus signal that you want to initialize. One of the model arguments (CounterICs) is a parameter structure that matches the shape of the output signal. This structure was defined as follows:

2. Open the dialog box for the root outport and enter the name of the structure (CounterICs) into the Initial output field.

NOTE: In general, when initializing a bus signal or state, the parameter structure does not need to match the bus type exactly, but its fields must be a subset of the elements in the bus object and the attributes of these fields must match the elements in the bus object.

Referenced Model: Masking Referenced Model

It is often useful to mask the referenced model to customize the user interface. You can create a mask on a model and then reference it from a Model block. To mask a model, click Create Model Mask in the model workspace section of Model Explorer for the model.

When you reference a masked model from a Model block, a mask is generated automatically on the Model block. For more information, see the topic Create and Reference a Masked Model.

Top Model: Parameterizing Model Blocks

The referenced model is configured to accept a structure for its model argument. This example calls each instance of the referenced model with different parameter values.

Top Model: Scheduling Calls to the Referenced Model

As mentioned earlier, the referenced model implements a limited counter algorithm with two trigger inputs. The algorithm detects "rising edges" in the trigger inputs and reacts as follows:

Resets the counter if the first trigger input changes.

Increments the counter by a specified amount if the second input changes.

In this example, you drive all three instances of the counter algorithm with the same trigger inputs, generated by the Stimulus subsystem. The period and sample time of the trigger inputs are defined by the values entered for the mask parameters of the Stimulus subsystem:

Reset counter every 4 seconds.

Increment counter 5 times per second (period = 0.2 second).

Sample time is 0.1 second.

NOTE: Reset and increment periods must be at least 2 times the sample time.

Simulating the Top Model

1. Save or close the referenced model.

You need to save the referenced model before it can be used by the top model. Alternatively, you can close the referenced model and use the original version of the model provided with this example.