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.)
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.
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:
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.
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 = 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.
This model uses
Dataset format for signal logging. To access Dataset format logged data for a given signal, use the getElement method:
ans = Simulink.SimulationData.Signal Package: Simulink.SimulationData Properties: 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
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
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 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.
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:
Click the Import button on the Root Inport Mapping toolstrip.
In the Import popup, select the Base workspace radio button.
In the Import popup tree, select the logged data
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:
Click the Signal Name radio button.
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_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
subOut.getElement('INNERDATA') is the same.
Create the executable and inspect the code for