Load Bus Data to Root-Level Input Ports
You can import bus data to top-level input ports by manually specifying the data in theInputconfiguration parameter or by using the Root Inport Mapper tool. For information about importing bus data using the Root Inport Mapper tool, seeImport Bus Data.
Imported Bus Data Requirements
You can import bus (virtual, nonvirtual, or array of buses) data to a top-level input port defined by a bus object (seeSimulink.Bus
). In the top-levelInportblock, setData typetoBus
and specify the name of a bus object. To specify data values for bus signals, use a structure of:
MATLAB®
timeseries
objectsMATLAB
timetable
objectsA combination of
timeseries
andtimetable
objects
Bus elements for which you do not include a field in the structure use ground values. You can use an empty matrix to specify to use ground values.
Note
When you specifytimetable
data to load, thetimetable
must contain data for only one signal.
The structure oftimeseries
ortimetable
(or both) objects must match the bus elements in terms of:
Hierarchy
Name of the structure field, which must match the bus element name. (The
name
property of thetimeseries
object does not need to match the bus element name.)Data type
Dimensions
Complexity
结构字段的顺序not have to match the order of the bus elements.
You can include the structure as an element of aDataset
object. You can use a structure in a comma-separated list. You can specify an empty matrix in a comma-separated list. The empty matrix uses the ground values for the bus signal.
For example, to load data for input portsin1
andin3
, and to use ground values for portin2
, enter the following in theInput参数:
in1, [], in3
Note
If you use a structure of MATLABtimeseries
objects for a rootInportblock in a model with multiple rootInportblocks, all rootInportblocks must use MATLABtimeseries
ortimetable
objects. Convert any rootInportblock data that usesSimulink.TsArray
orSimulink.Timeseries
objects to MATLABtimeseries
objects.
Initialize Bus Signals
You can initialize bus signals, including using partial specification of initialization data. For details, seeSpecify Initial Conditions for Bus Signals.
For details about importing array of bus data to a rootInportblock, seeImport Array of Buses Data.
Limitations for Importing Bus Data to Top-Level Inputs
The signal data that you can use the Root Inport Mapper tool to import and map to a top-levelInportblock can include bus data. You cannot use that tool to map bus signals to a top-level Enable or Trigger block.
You cannot use input ports to import buses in external modes. To import bus data in rapid accelerator mode, useDataset
format.
Import Bus Data to a Top-Level Inport
This model has twoInportblocks connected toScopeblocks. The data type of theIn1
block is inherited (nonbus data) and the data type of theIn2
block is defined by the bus objectBusObject
. The model has a callback that loadsBusObject
and its sub-busBusObject1
.
TheBusObject
bus object has two elements:
c
s1
, which is a nested bus that has two elements:a
b
Open the
model
.创建一个MATLAB
timeseries
object forIn1
, for which you want to import nonbus data.For example:
t1 = (1:10)'; d1 = sin(t1); in1 = timeseries(d1,t1);
创建一个n input structure, which can consist of MATLAB
timeseries
objects or MATLABtimetable
objects, or a combination of those types of objects. Create onetimeseries
ortimetable
object for each leaf bus element for which you do not want to use ground values. This example uses ground values for theb
bus element, so it does not need atimeseries
ortimetable
object for that element.t2 = (1:5)'; d2 = cos(t2); in2.c = timeseries(d1,t1); in2.s1.a = timetable(seconds(t2),d2);
The MATLAB
timeseries
objects that you create must match the corresponding bus elements, as described inImported Bus Data Requirements.创建一个
Dataset
object and addin1
andin2
to the data set.ds = Simulink.SimulationData.Dataset; ds = ds.addElement(in1,'in1_signal'); ds = ds.addElement(in2,'in2_signal');
In theConfiguration Parameters>Data Import/Export>Inputparameter edit box, enter the
Dataset
objectds
.Simulate the model. The Scope block connected to In2 shows the imported bus data.
Get Information About Bus Objects
To determine the number of MATLABtimeseries
objects and data type, complexity, and dimensions needed for creating a structure oftimeseries
objects from a bus, use these methods:
Simulink.Bus.getNumLeafBusElements
Simulink.Bus.getLeafBusElements
For example, for the bus objectBusObject
:
num_el = BusObject.getNumLeafBusElements
num_el = 3
el_list = BusObject.getLeafBusElements
el_list = 3x1 BusElement array with properties: Min Max DimensionsMode SampleTime Description Units Name DataType Complexity Dimensions
el_list(1).Dimensions
ans = 1
Create Structures of Timeseries Objects from Buses
If you havetimeseries
objects defined, you can use them to create a structure oftimeseries
objects based on a bus object. Use the
function. For example, if you have definedSimulink.SimulationData.createStructOfTimeseries
timeseries
objectsts1
,ts2
, andts3
, and you have a bus objectMyBusObject
, you can use this command to create a structure oftimeseries
objects:
input = Simulink.SimulationData.createStructOfTimeseries(...'MyBusObject',{ts1,ts2,ts3});
The number oftimeseries
objects in the cell array must match the number of leaf elements in the bus object. The data type, dimensions, and complexity of eachtimeseries
object must match those attributes of the corresponding bus object leaf node.
Import Array of Buses Data
To import (load) array of buses data using a rootInportblock, use an array of structures of MATLABtimeseries
objects.
Note
You cannot use anEnable,Trigger,From Workspace, orFrom Fileblock to import data for an array of buses.
Full Specification of Data
You can use logged data for an array of buses signal from a previous simulation as roundtrip input to a root-level Inport in a subsequent simulation run. The logged data is a full specification of data for theInportblock.
If you construct an array of structures of MATLABtimeseries
objects to specify fully the data to import:
Specify the structure fields in the same order as the signals in the bus signals.
Do not include more fields in the structure than there are signals in the bus.
For leaf fields, match exactly the data type, dimensions, and complexity of the corresponding signal in the bus.
Partial Specification of Data
To specify partial data for array of buses, create a MATLAB array of structures with MATLABtimeseries
objects at the leaf nodes.
The structure that you create to specify partial data must be consistent with these rules:
You can omit fields, including leaf nodes and subbranches. You can also omit dimensions. If you do not specify a field, Simulink®uses the ground value for that field.
For nested bus nodes, make the dimension of each field equal to, or smaller than, the dimension for the corresponding node of the array of buses.
This example shows how you can specify partial data to be imported using a rootInportblock whose data type is defined as bus objectMyBus
. You can open the model (ex_partial_loading_aob_model
) and the MATLAB code that defines the data to import (ex_partial_loading_aob_data.m).
When you simulateex_partial_loading_aob_model
, you see:
Theinput
Inportblock uses theMyBus
bus object as its data type.
TheMyBus
array of buses includesMyBus(1)
andMyBus(2)
. The port dimension is set to 2 to reflect the two buses in the array of buses, andOutput as nonvirtual busis enabled.
Here are the elements of the array of buses, which includesMyBus(1)
andMyBus(2)
. The color highlighting shows the nodes of the array of buses for which data is being imported.
Here is MATLAB code that defines the data to import. The color that highlights the code matches the color of the corresponding node in the array of buses. To view the code used in this model, open the MATLAB code fileex_partial_loading_aob_data.m.
In the code that defines the import data:
The
timeseries
objectMyBusValue
specifies the data for the highlighted nodes.The
timeseries
objectBT
forMyBus(2)
, becauseBT
is a leaf node, it must match exactly the dimensions, data type, and complexity of the corresponding bus element.The structure specifies data for
Y(2)
. You can skip the first and last nested buses ofY
(that is,Y(1)
andY(3)
).
This example specifies data forY(2)
;您可以跳过第一个和最后一个嵌套的公交车Y
(that is,Y(1)
andY(3)
).
在你定义MyBusValue
variable for the import data, set theConfiguration Parameters>Data Import/Export>Inputparameter toMyBusValue
.
See Also
Related Examples
- Load Data to Root-Level Input Ports
- Import Bus Data
- Buses at Model Reference Interfaces
- Specify Initial Conditions for Bus Signals
- Import Array of Buses Data
- Bus Data Crossing Model Reference Boundaries