MATLAB Answers

Bus object for a Structure has so many duplicate buses. How to consolidate?

6 views (last 30 days)
I have a structure that has nested structures. I use Simulink.Bus.createObject to create a bus object so I can use it in my Simulink model. This all works fine, but I noticed that so many buses are created and many are just duplicates in their format. Is there a way to prevent all these duplicates from being generated or to consolidate them?
An idea of what I'm doing:
var.A.ID = 1;
var.A.type = 1; = 0; = 0;
var.B.ID = 2;
var.B.type = 2; = 0; = 0;
This will create the buses required to represent this struct variable in Simulink however a bus for the "data" variable is created twice. User gets "data" and "slBus1_data". And the bus for var.A uses "data" and the bus for var.B uses "slBus1_data". This creates overhead when using Simulink as this structure gets much larger and more complex.
Is there a way to consolidate identical buses without having to do it manually?


Sign in to comment.

Accepted Answer

Dhruvesh Patel
Dhruvesh Patel on 20 Mar 2017
The behavior which you are observing is expected because you are constructing the bus from a nested struct. Hence every time (twice in your case) the function 'Simulink.Bus.createObject()' finds a struct ('data' in your case) it creates a new Bus type for it and uses that in the main Bus.
Creating a Bus from a struct is a crude method and does not provide fine control over the creation of the Bus which is required for a nested Bus like yours. I would suggest creating the main nested-Bus by first creating its element-Buses. Refer the following code to get started :
% Required bus structure for the mainBus
%The bus object on left and the names of the elements in right inside ().
% mainBusType (mainBus) : LEVEL 0
% | .
% |-Atype .....(A) : LEVEL 1
% | |
% | |- uint32 (ID) : LEVEL 2
% | |- double (type) : LEVEL 2
% | |- MyDataType (data) : LEVEL 2
% | |
% | |- double (response) : LEVEL 3
% | |- double (validity) : LEVEL 3
% |
% |-Atype (B)
% | |
% | |- uint32 (ID)
% | |- double (type)
% | |- MyDataType (data)
% | |
% | |- double (response)
% | |- double (validity)
% Create a struct which will be used to create Bus for MyDataType (Level 2)
% Create a struct which will be used to create Bus for Atype (Level 1)
% Note how we have used the datastruct created above
% create the Bus Atype using the struct defined above.
% Creating the Bus elements (Level 1) of the mainBus (Level 0)
elems(1) = Simulink.BusElement;
elems(1).Name = 'A';
elems(1).Dimensions = 1;
elems(1).DimensionsMode = 'Fixed';
elems(1).DataType = ABBus.busName;
%Note the type : We are using the Bus created above using
% ABBus=Simulink.Bus.createObject(ABStruct);
elems(1).SampleTime = -1;
elems(1).Complexity = 'real';
elems(2) = Simulink.BusElement;
elems(2).Name = 'B';
elems(2).Dimensions = 1;
elems(2).DimensionsMode = 'Fixed';
%Note the type : We are using the Bus created above using
% ABBus=Simulink.Bus.createObject(ABStruct);
elems(2).DataType = ABBus.busName;
elems(2).SampleTime = -1;
elems(2).Complexity = 'real';
You can refer to the following documentation page for more information on this method of Bus creation.
As far as consolidating the types in already created Bus goes, I am not aware of any method to achieve that. But if you have the control over the creation of the Buses then you could use the method mentioned above to create the Buses correctly in the first place and avoid the problem of consolidating all together.
I hope this helps.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!