Dynamic wiring in linked block - matlab

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.

Related

How to store a specific time that a signal value is changed in another parameter in Matlab simulink?

I have two Matlab function blocks that one is producing zero signal for the other. I want to store the exact time that signal changes to one in another parameter in order to use it elsewhere.
How can I do it?
This type of functinality is achieved using a Triggered Subsystem, as per the image below,
The output of the trigger block will take on value of the input (which in this case is the simulation time) every time the trigger signal rises. Look at the parameters of the Trigger block inside the subsystem for other options such as falling edge, or both edge, triggering.

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.

Any Tic Toc function in Simulink for embedded blocks

I have a system with some embedded Matlab blocks where I'd like to perform some actions after a certain amount of time, in this case turn on lights and switches in an interface to which I send signals from Simulink.
The problem is that I thought I'd use "tic"-"toc" and "while" in a Matlab function block to perform these actions, say one parameter becoming 1 after 5 seconds, the following parameter becoming 1 after 12 seconds and so on, but I noticed that tic-toc apparently doesn't work in Simulink for embedded functions.
Is there any similar functions that could be used in Simulink for embedded functions or is there any other way to do this?
Edit: I've tried to get the clock's time as well, but it's a growing value. Is there any way to "lock" the time as a parameter when the block's function is executed?
You shouldn't be using absolute time in an embedded system, which is at least one of the reasons why tic-toc and clock from MATLAB don't work with Simulink Coder.
You should create your own counter, which you start and stop when you need to.
This is pretty easy to do using a Unit Delay and Summation block.
If you need to be able to enable and/or reset the counter then use the appropriate block from the Additional Discrete library.

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.

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.