Main Content

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 data dictionary file named sldemo_mdlref_bus.sldd that contains bus objects that define the elements of COUNTERBUS, LIMITBUS and INCREMENTBUS.

Note: You can view these bus objects in the Model Explorer by opening the data dictionary from the Data tab in the dialog pane and selecting its Design Data in the Model Hierarchy pane. To view these bus objects in the Bus Editor, select one of them in the Model Explorer and click Launch Bus Editor in the dialog pane. 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 = 

Simulink.SimulationData.Dataset 'topOut' with 4 elements

                         Name          BlockPath                                
                         ____________  ________________________________________ 
    1  [1x1 Signal]      COUNTERBUS    sldemo_mdlref_bus/Concatenate           
    2  [1x1 Signal]      OUTERDATA     sldemo_mdlref_bus/CounterA              
    3  [1x1 Signal]      INCREMENTBUS  sldemo_mdlref_bus/IncrementBusCreator   
    4  [1x1 Signal]      INNERDATA     ...erA|sldemo_mdlref_counter_bus/COUNTER

  - Use braces { } to access, modify, or add elements using index.

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')
  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

To log signals in the referenced model, select the Model block and click Log Signals on the Model Block tab.

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 all signals 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 Connect Input button. This action opens the Root Inport Mapper 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 From Workspace button in the Root Inport Mapper toolstrip.

  2. Select the logged data topOut and click the OK button.

Now that the data is loaded into the Root Inport Mapper 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. In the Root Inport Mapper hierarchy pane, select scenario dataset topOut.

  2. Click the Signal Name radio button.

  3. From the Map to Model button dropdown, click Map All.

When mapping data, Simulink evaluates inports against the input data to determine compatibility. The Root Inport Mapper table reflects the status of this compatibility with a green check mark, orange warning triangle, or red error exclamation mark. 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.