Extracting available Bus signals in a nested Bus recursively.

12 次查看(过去 30 天)
I have some nested Bus objects in the workspace which are similar to the example "Create Bus Objects from Bus Element Objects" (https://de.mathworks.com/help/simulink/slref/simulink.bus.html). Now I am trying to access the available signal names by using a recursive function.
This is the code:
function signals = extract_bus_signals(bus_obj)
signals = {};
tempVal = bus_obj.Elements;
i_end = size(tempVal,1);
% loop through each element in the bus object
for i = 1:i_end
% check if the element is a bus object
if contains(string(tempVal(i,1).DataType),'Bus')
nested_bus_name = extractAfter(tempVal(i,1).DataType, 'Bus: ');
extract_bus_signals((nested_bus_name));
else
% add the name of the signal to the overall signals list
signals{end+1} = bus_obj.Elements(i).Name;
end
end
The error that I am getting is :
Dot indexing is not supported for variables of this type.
Error in extract_bus_signals12 (line 5)
tempVal = bus_obj.Elements;
Error in extract_bus_signals12 (line 14)
extract_bus_signals12((nested_bus_name));
I believe I am getting this error because when I pass 1st nested Bus name( 'NestedBus' ) which I am getting at ( "nested_bus_name = extractAfter(tempVal(i,1).DataType, 'Bus: ');"), which is a "string" to "bus_obj". instead of a bus. Any ideas to how to pass the child bus as bus object when calling the function.
My understanding might be wrong. Any guidance is appreciated.
Thank you

回答(1 个)

Oguz Kaan Hancioglu
Since it is defined is yor base workspace, you can use this,
Mathias Benedek (2023). NSTRUCT2CELL (https://www.mathworks.com/matlabcentral/fileexchange/29908-nstruct2cell), MATLAB Central File Exchange. Retrieved February 25, 2023.
  2 个评论
Anubhav Routray
Anubhav Routray 2023-2-25
Unfortunately, it is not working for Bus objects. Its just keeping the Bus object in a cell. And clicking on the cell opens the Bus editor
Oguz Kaan Hancioglu
Before using the nstuct2cell I used following commands,
portHandles = get_param('bus creater location in sumulink (use gcb)','PortHandles')
After you check the handle of bus creater, you can convert the struct,
portStruct = Simulink.Bus.createMATLABStruct(portHandles.Outport)
you can feed the portStruct to NSTRUCT2CELL (nstruct2cell(portStruct))
Best

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by