Simulink math operations on 2 bus signals - matlab

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:

Related

Converting [Nx1] signal to N separate signals in Simulink

I am using the following UDP receiver block for the Parrot Mambo drone,
Which outputs an [2x1] array of singles in my case. However, I would like to split the output into two separate signals. What block should I use for this?
It was easier than I thought, but nevertheless hard to find on the internet. The solution is to use the demux block.

What is the meaning of these numbers in Simulink?

Consider following parts of a Simulink model. (Note that I removed the content of the blocks.)
I want to know the meaning of the numbers
above and below the arrows: 14{14}, 25, [25x121]
in the top right-hand side of the blocks: 0:B, 0:8.
The 14{14}, 25 and [25x121] above the signals are the signal dimensions:
In the case of 25, it's a single vector signal of width 25;
In the case of [25x121], it's a matrix signal of dimensions [25x121];
In the case of 14{14}, it's a bus signal with 14 signals carried in the bus and 14 signal elements carried in the bus (i.e. each signal is of width 1)
Consider the following example in the Simulink documentation:
For the other numbers inside the block, they represent the sorted execution order of the model, i.e. in which order each block gets executed. It's difficult to explain what each number is without seeing the whole of the model and copying the documentation verbatim (the notation changes depending on what type of block it is and its level in the model hierarchy) , so I'll just refer you to the documentation page, which should provide all the necessary explanations.

Simulink large scale modeling: best practices for interconnecting blocks

What are the best practices for large scale modeling in Simulink when it comes to connecting blocks? Would you use the same structure for all I/O ports of your blocks to facilitate their interconnection (but obviously there will be a lot of redundant signals) or would you define custom structures for each I/O port type with only the necessary information?
For example:
A reactor is modeled as a single block with 4 inputs and 1 output:
I1. Feed which is a structure containing: Flow and Concentrations (7 species);
I2. Mass flow of enzymes - scalar;
I3. Mass flow of water - scalar;
I4. Outflow - which is adjusted by a controller to keep a constant mass in the tank - scalar;
O1. The outstream, which is a struct: Flow and Concentrations
(let's say 10 species).
Now imagine this reactor block is only a tiny piece of an entire process. There are enzymes and water tanks connected to it and some other downstream processes etc.
Would you use a unique structure for all IO ports (even if it scales up to 50-100 components but you would need less per block or 1 component like I2, I3 and I4 above which are scalars)? Is this regarded as bad programming practice?
Or would you customize the IO port structure for each block? Of course you would group them somehow and make reuse of them but with no redundant information.
Thanks!
You might find the following useful: http://www.mathworks.co.uk/videos/tips-and-tricks-for-large-scale-model-based-design-part-2-81873.html.
I would personally use a single bus input and a single bus output for your reactor block. You can then group buses together to form larger bus signals as you move up the hierarchy of your model. Look at the Bus Creator and Bus Selector blocks.

Append Signal to Bus in Simulink

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.

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.