Clear Filters
Clear Filters

How to make a function in script which can handle the bus inside bus inside bus signals and add the bus creator and bus selector pairs according to the signals ?

14 views (last 30 days)
Suppose I have one block in which there is one bus signal coming for example 'main_bus' now this bus signal has 5 to 6 signals in it and some of the signals are 'A.B.Signal1', 'A.B.Signal2', 'A.C.Signal3', 'B.Signal4', 'B.Signal5', 'Signal6', 'Signal7' So, here we know that there are mainly four signals 'A', 'B', 'Signal6' and 'Signal7'.
Signals look like below
So, I want to make one function(script code) which will add one main Bus selector and Bus creator pair for the main input signal 'main_bus' and then it will select all these 4 signals on output side. So, the function should start checking each 4 signals one by one. First it will read the first signal 'A' if that is bus signal then it should add one more bus creator and bus selector pair and then select the signals which are inside that bus 'A' and again do the same till it reached the end signal in sub-bus
'B' :- 'Signal1' and 'Signal2' and
'C' :- 'Signal3'
and then take next bus 'B' and again do the same as it did for bus 'A'
So, at the end one simulink block should look like
So, can there be any recursive function written in script which will do this task like a generalised with n number of bus inside bus inside bus.. structure ?
the result must look like this using code lines.

Answers (1)

Yatharth
Yatharth on 6 Sep 2023
Hi Shiv,
I understand that you want a recursive function written in script to create Bus_Selector and Bus_Creator pairs for a nested Bus System.
You can implement the same using a recursive function in MATLAB:
  1. Define the function with appropriate input and output arguments. Let's call it handleBusInsideBus
  2. Inside the function, iterate over each signal in the input bus using a loop.
  3. Check if the signal is a bus using the isstruct function. If it is, recursively call the handleBusInsideBus function to handle the nested bus.
  4. If the signal is not a bus, add the necessary bus creator and bus selector pairs to the output bus. You can create a new structure to store the bus creator and bus selector pairs.
function output_bus = handleBusInsideBus(input_bus)
output_bus = struct('BusCreator', {}, 'BusSelector', {});
input_cell = struct2cell(input_bus);
for i = 1:length(input_cell)
signal = input_cell{i,:};
if isstruct(signal)
output_bus_nested = handleBusInsideBus(signal);
output_bus = [output_bus, output_bus_nested];
else
% Process non-bus signals
output_bus(end+1).BusCreator = signal;
output_bus(end).BusSelector = signal;
end
end
end
5. Finally, you can call the handleBusInsideBus function with the initial input bus
% Define the input bus
input_bus = struct( 'A' , struct('AB',struct('S1', 1 , 'S2',2), 'AC' , struct('S3' , 3) ) , 'B', struct('S4' , 4 , 'S5' , 5) , 'S6' , 6 , 'S7' , 7);
% Call the handleBusInsideBus function
output_bus = handleBusInsideBus(input_bus);
Note: you can modify the data structures and set your desired output accordingly, the same recursive approach can be modified if you want to store the sequence of the Nested Bus Selector.
I hope this helps.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!