Variable transport delay using time after threshold has been reached in Simulink - matlab

In Simulink how can I delay the transport block to the exact time of when a certain threshold has been reached?
Basically I have an S-function which switches to an alternate input source, a sine wave, after a certain value has been reached. As soon as the switch occurs I want to start the sine wave. For that I need to delay the wave generation until the switch occurs.
How can I send that time to the Variable transport delay block?

Maybe you can try this way:
you need Variable Integer Delay for example. It changes delay to what you need. Calculate needed value of delay you can in User Defined Function, route to it your threshold value and current time and all other values you need.
Hope i understood your question correctly!

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.

Is it possible to stop a Simulink simulation when a certain condition is met?

Assume that you have a Simulink simulation where a certain signal is first positive and after some time t in a given interval, it becomes negative. Your goal is to find the zero-crossing.
A first approach would be to plot the signal over the given interval, save it and calculate the zero-crossing.
When repeating this simulation numerous times for varying parameters, it would be beneficial to be able to stop the simulation after the signal has become negative. Then there is already enough information for the calculation of the zero-crossing in Matlab. How could you do that?
Yes, use the Stop Simulation block with the appropriate logical input to the block:
You can use an if / else block to control the flow in the Simulink model. In the if / else block, you can choose the condition if u > 0, continue as normal if it's true, and use the else-option to bypass the rest of the flow you would otherwise run. For instance jump directly to the scope.
Another ways:
You can use Hit Crossing Block in Simulink to find time at the moment of hitting zero.
Another way - use any Trigger or Resettable system. It detects the zero crossing too. For example: this question at SO.
Of course you can also use User Defined function to detect zero crossing by your hand and do whatever you want with the signal at the same time.
About making a lot of simulations and then stops:
you can use Check Upper Static Bound for automatically stops simulation at the moment when zero will be crossed in nth time. For example:
I set upper bound = 10 for this block and this stops at 10th crossing.
There are a lot of ways to save function values in this points or just array of times but this is an another question :)

how to store incoming data and then output it after a certain delay in simulink

I have an incoming data and I would like to store it and then Output this data but after a certain delay, after some milliseconds i Output this data.
I used the Queue block inside an enabled Subsystem and the Trigger Signal is the clock divided by 10, So i have evrery time .. every 0.1 second i Output the values from the block,.. but the data is accumulated, not delayed. any idea why?
Here is the Picture of this Operation
and
EDIT: You now show how are you storing the signal. And The queue block is used wrongly.
If you want just to delay a signal, and output it delayed, then use my answer below. I am unsure what you mean by store it by N time and then output it. Simulink is "continuous" thus you can not output it "in one go" after N time, that makes no sense. The closest thing to that is to delay the singal, and for that, you dont need that enabled subsystem, you just need the Transport delay block.
ORIGINAL
What about the Transport Delay block?
It looks like this:
and It allows you to set the delay time in seconds, isntead of in ticks (as z^-1 does).

Unit delay's magnitude

What is the magnitude of delay in sec offered by unit delay (simulink block) for following condition?
Configuration parameters are variable step with ode45 solver.
Max step size is 1/60 and min step size is auto.
In delay block sample time is kept as 0.5s.
Input processing is set as 'Elements as channels (sample based)'.
Thanks
I hope you are using discrete blocks as you have mentioned "Unit Delay". In that case, the delay caused is your sample time. ie here it is 0.5s.
Usually Matlab will go for a discreet solver unless you have explicitly mentioned 'ode45' solver. Also, stepsize of solver wont affect the delay
Simply your sampling time. But If you have some informations that we do not know I will give you a way to measure it.
In you model add 2 Scopes. Assign one of these Scopes' input as output of the Delay Block. Other Scope's input will be input of the Delay Block.
Run the simulation and open the graph results of Scopes. There you go. You can see how much delay your Delay Block caused.

Simulink: How to convert event based signal with zero duration values to a time based signal without losing information

I have a matlab function block (which is not relevant) whose input is his previous output (loop). For example, if in a sample period the output is X, his input in the next sample period will be X, and so on.
This image shows a simplification of my simulation. I initialize the input of my function for the first loop.
The problem is that matlab functions recieves an event based signal from de initialization block in the first sample period (zero-duration), which I must convert to a timed based signal (so I can apply the unit delay that avoids an inifite loop, and allows to generate the next input as explained before). So, when I do so, I lose the information contained in the event-based signal (due to the zero-duration values) and the loop does not work.
If there was a way to intialize the loop in the time-based domain (green part of the image) so, in the first sample time, it is not a zero-duration signal, it would avoid the problem.
Is there any way to do so? Or, a different approach for this problem?
Two approaches come to mind
The initial condition can be set in the Unit Delay block, so it's not clear from your simplified example why you need the specific Initialization block.
You could just use a persistent variable inside the MATLAB Function block to maintain the state from one execution of the block to the next (noting that since it is event driven the block may not get called at every time step, only at each event triggger).