MATLAB: Selecting bus signal from variable in Simulink

busedit busselectorsimulink

Hello,
I have a question regarding the selection of a specific bus signal. The selection is not the same from time to time, thus the "Bus selector"-block can not be used.
What I'm looking for is something that corresponds to the "Selector"-block for vectors where the ability to select an index is possible with the use of scalar (or some index).
I've defined my own bus object as e.g. A = [foo foo1]. A.foo = [10 11]; A.foo1 = [20 21];
During simulation I woluld like to make the selection of both e.g. A.foo(1) or A.foo1(2).
I believe there is a solution to this, since a bus object can be viewed as a data struct, and one can solve it via a matlab-script by e.g. Say that A is indata, and defined as before:
"function out = bus_selector(A)
WantedValue = SelectorVariable.FieldNumber WantedField = find(fieldnames(A),SelectorVariable.FieldName);
Out = A.{WantedField}(WantedValue)"
Thus, how can I make this possible in Simulink?
Any help much appreciated. Thank you
Sincerely, Pär Hammarlund

Best Answer

AFAIK, this is not possible in Simulink. The reason that dynamic selection of bus elements cannot be supported is because bus signals are an aggregate of signals that can each be of different datatype, complexity or dimensions - this means that the datatype/complexity of the output of the Bus Selector cannot be determined up-front at compile-time. Currently it is only possible to vary the dimensions of a signal, but not datatype/complexity, during model run-time. If all your bus elements have the same properties (and are either scalars or 1-D vectors), then you can use the "Bus to Vector" block to flatten out your bus, and then use the regular Selector block to select signals dynamically.