Extracting available Bus signals in a nested Bus recursively.
17 views (last 30 days)
Show older comments
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1306225/image.png)
0 Comments
Answers (1)
Oguz Kaan Hancioglu
on 25 Feb 2023
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 Comments
Oguz Kaan Hancioglu
on 27 Feb 2023
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
See Also
Categories
Find more on National Instruments Frame Grabbers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!