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.)
|On this page…|
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.
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:
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.
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 Package: Simulink.SimulationData Characteristics: Name: 'topOut' Total Elements: 4 Elements: 1: 'COUNTERBUS' 2: 'OUTERDATA' 3: 'INCREMENTBUS' 4: 'INNERDATA' -Use 'get' or 'getElement' to access elements by index or name. -Use 'addElement' or 'setElement' to modify elements.
ans = Simulink.SimulationData.Signal Package: Simulink.SimulationData Properties: Name: 'COUNTERBUS' BlockPath: [1x1 Simulink.SimulationData.BlockPath] PortType: 'outport' PortIndex: 1 Values: [2x1 struct]
Bus data is logged as a MATLAB structure in the Values field:
2x1 struct array with fields: data limits
This structure contains MATLAB timeseries objects for each bus signal:
>> topOut.getElement('COUNTERBUS').Values(1).data and >> topOut.getElement('COUNTERBUS').Values(2).data
timeseries Common Properties: Name: 'data' Time: [301x1 double] TimeInfo: tsdata.timemetadata Data: [301x1 int32] DataInfo: tsdata.datametadata timeseries 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:
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.
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 specifies 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.
You can simulate sldemo_mdlref_counter_bussimulate 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.