Append Signal to Bus in Simulink - matlab

Is there anyway to simply append a signal to a bus in Simulink? This doesn't seem to be that difficult, but I haven't found a nice way to do it. I can only think of two ways to currently accomplish this:
Select all signals in the bus with a bus selector, then create a bus with all those signals plus the signal to be append using a bus creator. (this is what I am currently doing)
"Pre-allocate" a space in the bus for the signal to be append beforehand then simply replace its value with the value of the actual signal using a bus assignment block.
Is there a reason as to why Simulink cannot append a signal to a bus? Is it because on the backend they are represents as structures (or MATLAB's equivalent) and a property cannot be added to a structure at runtime?

If you are using a bus object, you can use the Bus Editor to add a bus element to an existing bus.

Related

How to get bus element information in Matlab programmatically?

Say there are Simulink buses in a model, Bus1 and Bus. How can we access the Bus1 info like which elements it has, their dimensions etc.
You can query data about buses by looking at the Signal Hierarchy or Compiled Bus Type of a signal.
However it appears that if you only have a virtual bus, you can only get the signal names and not the types/dimensions etc. The compiled bus type for a virtual bus is "VIRTUAL_BUS" so no information is provided.
This page discussed how to programmatically query a signal on a bus but as stated above, you will only find information, if you have a non-virtual bus.

Simulink math operations on 2 bus signals

I need to make a Simulink block which receives a concatenation of a number of bus signals and performs the same math operations on the signals contained in the bus for each pair of consequent buses. The bus signals are of the same type and are non-virtual.
For the sake of the question, let's assume to have a concatenation of 4 simple buses, each containing a x and a y field. A bus of signals composed of a=x1+x2 and b=y1-y2 need to be made out of buses inputs 1,2 and 3,4. So, the output of the block should be a concatenation of 2 buses, the first containing information from the first pair of input buses, and the second one from the second pair.
An hard-to-scale way to do it is the following.
Are there any built-in Buses Math operations possibilities, or better ways to implement this? I could not find anything in Mathworks documentation, and simple operations block generate incompatibility errors.
You need to use For Each Subsystem Block. As shown in this example. Note that I called the bus BusTest and made the dimension and datatype of signals visible:
Now set the Signal Width parameter of that block to two so it divides input array into chunks of length 2:
Then move your logic into that block:

Simulink bus selector after add block

In simulink, you can name a signal and in bus creator you combine them and in bus selector you can decompose them again. The problem is when you add two set of signals to each other. Then, the output of add must be combined signals.
Now, my problem is how to name them after (1 by 1) Add in the way that I can select them from bus selector again?
You can't, it doesn't make sense. Once you have added two signals together, you have a third signal, which you will need to (manually) name. You can't use a bus selector block on the output of an add block to retrieve the initial input signals to the add block.

Dynamically sized bus objects in Simulink

I wrote a C S function which has a variable number of states depending on one parameter, which is passed to it (I'm using computational fluid dynamics and the parameter is the number of cells). I want to output a bus object from my S function that contains a temperature profile. Problem is I don't know the length of the output when I create the bus object in Simulink (in Bus Editor). Is there a way to dynamically set the size of the bus object from the C S function?
I think you can set the DimensionsMode property to "variable" instead of "fixed" (the default). See Simulink.BusElement and Variable-Size Signal Basics in the documentation for more details. Not sure how to code this in the S-function though.

Dynamic wiring in linked block

The problem:
In my simulink model, I have a bus creator that will send all the signals to a block. The block purpose is to make a selector and a switch dynamically based on an index. Example : I have 3 objects, each with a position and a velocity signal, and I want to get the position of the 3 objects, so 3 signals (this means the bus selector will have 3 output and the multi switch will have 4 (one for the index)). The block is working correctly, with a recursive function script called in the callback, it checks the bus for all possible signals and count the number of objects, and modifies the bus selector and the switch accordingly. The problem is that this block is linked to a library, so I have to disable the link in order for the callback script to work.
The questions:
What should i change in order to avoid disabling the link?
Is there another way of making dynamic wiring and don't use a callback script?
I suggest a using vector concatenation block instead of bus creator plus a matlab function block to choose. Here is the model
and here is the code in matlab function:
In this way the block in the library does not need to change each time. Also if you have to keep your buses you can use a bus to vector block to convert them to vector.
The idea of using a MATLAB Function block to do the selecting is a good one, but I would try and stay with a Bus input to preserve the advantages of working with busses, rather than convert to vector and back. So instead of having your code as a callback to the bus selector block, my suggestion would be to implement it in a MATLAB Function block with a bus input and the signal input used to do the selection in the bus, and define the output still as a bus object.