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;
var.A.data.response = 0;
var.A.data.validity = 0;
var.B.ID = 2;
var.B.type = 2;
var.B.data.response = 0;
var.B.data.validity = 0;
Simulink.Bus.createObject(var);
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?

  0 Comments

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)
field11='ID';
field12='type';
field13='data';
field131='resonponse';
field132='validity';
% Create a struct which will be used to create Bus for MyDataType (Level 2)
dataStruct=struct(field131,0,field132,0);
% Create a struct which will be used to create Bus for Atype (Level 1)
% Note how we have used the datastruct created above
ABStruct=struct(field11,int32(0),field12,0,field13,dataStruct);
% create the Bus Atype using the struct defined above.
ABBus=Simulink.Bus.createObject(ABStruct);
% 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';
mainBus=Simulink.Bus;
mainBus.Elements=elems;
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)

Sign in to answer this question.