Interface Specification Using Bus Objects

This example shows how to propagate bus signals into referenced models. It also shows how you can simulate the referenced models independently using logged signal data from the parent model. (For a quick introduction to Model Reference, see Component-Based Modeling with Model Reference. See Simulink® documentation for more information about configuring signals for data logging.)

Open The Example Model

Open the example model

Example Requirements

During this example, Simulink and Simulink® Coder™ may generate code in the code generation folder created in the current working directory. If you do not want to (or cannot) generate files in this directory, you should change the working directory to a suitable directory. Simulink Coder is required to generate code to be deployed in standalone applications.

Description of Bus Signal Setup for the Example

This model, sldemo_mdlref_bus contains a Model block, CounterA, that references sldemo_mdlref_counter_bus, a model of a simple counter. A bus signal called COUNTERBUS feeds the data and the saturation limits of the counter into the model. COUNTERBUS contains two signals: the data and LIMITBUS. The data signal is used in counting, and LIMITBUS contains the upper and lower limit values of the counter. The following shows the structure of COUNTERBUS:

  * COUNTERBUS (bus signal)
             * data
             * LIMITBUS (bus signal)
                      * upper_saturation_limit
                      * lower_saturation_limit

The Model block CounterA contains a second input port that feeds a bus signal of type INCREMENTBUS. This signal is used to change the increment and reset the counter. The following shows the structure of INCREMENTBUS:

  * INCREMENTBUS (bus signal)
               * increment
               * reset

The example also includes a MAT-file named sldemo_mdlref_counter_bus.mat that creates bus objects that define the elements of COUNTERBUS, LIMITBUS and INCREMENTBUS. The preload function of sldemo_mdlref_bus loads this MAT-file when the model is loaded, causing the bus objects to be created in the MATLAB® workspace.

Note: You can view these objects by selecting the Base Workspace in the Model Explorer. You can also view the bus structures in the Bus Editor, which you can open by typing buseditor at the MATLAB command prompt. The model uses these bus objects to specify the outputs of the following Bus Creator blocks:

  • COUNTERBUSCreator1

  • COUNTERBUSCreator2

  • LIMITBUSCreator1

  • LIMITBUSCreator2

  • IncrementBusCreator

The Inport counter_input of the referenced model sldemo_mdlref_counter_bus specifies the COUNTERBUS bus object in its Data type field on the Signal Attributes tab. The Inport increment_input is similarly configured to use the bus object INCREMENTBUS.

Logging Signal Data

Now you can simulate sldemo_mdlref_bus to see the output in the scope window.

Note that the following signals have been marked for signal logging:





After the model simulates, the logged signal is available in the Base Workspace in the topOut variable.

topOut = 

  Package: Simulink.SimulationData

              Name: 'topOut'
    Total Elements: 4

    1 : 'COUNTERBUS'
    2 : 'OUTERDATA'
    4 : 'INNERDATA'

  -Use 'get' or 'getElement' to access elements by index or name.
  -Use 'addElement' or 'setElement' to modify elements.

This model uses Dataset format for signal logging. To access Dataset format logged data for a given signal, use the getElement method:

   >> topOut.getElement('COUNTERBUS')
ans = 

  Package: Simulink.SimulationData

              Name: 'COUNTERBUS'
    PropagatedName: ''
         BlockPath: [1x1 Simulink.SimulationData.BlockPath]
          PortType: 'outport'
         PortIndex: 1
            Values: [2x1 struct]

Bus data is logged as a MATLAB structure in the Values field:

   >> topOut.getElement('COUNTERBUS').Values
2x1 struct array with fields:


This structure contains MATLAB timeseries objects for each bus signal:

   >> topOut.getElement('COUNTERBUS').Values(1).data
   >> topOut.getElement('COUNTERBUS').Values(2).data

  Common Properties:
            Name: 'data'
            Time: [301x1 double]
        TimeInfo: tsdata.timemetadata
            Data: [301x1 int32]
        DataInfo: tsdata.datametadata


  Common Properties:
            Name: 'data'
            Time: [301x1 double]
        TimeInfo: tsdata.timemetadata
            Data: [301x1 int32]
        DataInfo: tsdata.datametadata

You can also plot a logged data using the plot command:

   >> topOut.getElement('OUTERDATA').Values.plot()

Logging Model Reference Signals

When simulating the top model sldemo_mdlref_bus, you can select the signals in the reference model sldemo_mdlref_counter_bus to log using the Signal Logging Selector. Right-click the Model block CounterA in the top model and select Signals & Ports > Log Model Reference Signals... to choose which signals from the referenced model to log.

Note that for this model, the logging mode Log all signals as specified in model is selected. This means that all the signals that are logged when simulating sldemo_mdlref_counter_bus standalone are logged when simulating the top model. To change logging for any of these signals or to log a subset of signals:

  • Set the logging mode to "Override signals".

  • Uncheck the top-level model block on the left hierarchy view to indicate that this model should not use "log as specified in model".

  • Use the list on the right side to specify which signals to log when simulating the top model.

  • To save these changes, save the top model.

Loading Data

Open the referenced model sldemo_mdlref_counter_bus

The referenced model sldemo_mdlref_counter_bus has been configured to read the logged signal data through the root Inport blocks. You can see how this is done by opening the Configuration Parameters dialog of the referenced model and selecting the Data Import/Export tab. The Input field is configured to read the logged data from topOut for both input ports. A comma-separated list specifies values for the 2 input ports. The increment_input port uses a structure of timeseries to load data for INCREMENTBUS and the counter_input port uses an array of structure of timeseries to load data for COUNTERBUS.

  • topOut.getElement('COUNTERBUS')

  • topOut.getElement('INCREMENTBUS')

An easy way you can configure the Input field is by clicking the Edit Input button. This action opens the Root Inport Mapping tool. In this example, this tool uses a mapping algorithm to set up the Input field from logged data in the base workspace.

To select the data to import:

  1. Click the Import button on the Root Inport Mapping toolstrip.

  2. In the Import popup, select the Base workspace radio button.

  3. In the Import popup tree, select the logged data topOut.

Now that the data is loaded into the Root Inport Mapping tool, you can determine the root input port for which to assign input data. Simulink matches input data with input ports based on one of five criteria (block name, block path, signal name, or a custom algorithm). Since topOut was logged using signal names from the model, the best choice for a mapping criteria is Signal Name. Using this criteria, Simulink tries to match input data variable names to the names of the input signals.

To select this option:

  1. Click the Signal Name radio button.

  2. Click the Map button.

When mapping data, Simulink evaluates inports against the input data to determine compatibility. The Root Inport Mapping table reflects the status of this compatibility in green, orange, or red. In this example, the table shows a green status indicating there is no compatibility issue. You can simulate the model with this mapping of input data to inports. The Map action also sets the Input field in the Data Import/Export tab of the Configuration Parameters dialog with the proper comma separated list of inputs. To apply the changes to the model, in the Configuration Parameters dialog, click Apply.

You can simulate sldemo_mdlref_counter_bus to see the output in the scope window. After simulation, the scopes from sldemo_mdlref_bus and sldemo_mdlref_counter_bus show the same trace. The signal feeding the scope in the referenced model is also logged. The logged data is available in the MATLAB workspace under the variable subOut. You can verify that the data in topOut.getElement('OUTERDATA') and subOut.getElement('INNERDATA') is the same.

Code Generation For Standalone Applications (Requires Simulink Coder)

Create the executable and inspect the code for sldemo_mdlref_bus.


Close sldemo_mdlref_counter_bus and sldemo_mdlref_bus.

Was this topic helpful?