Variable storage in simulink - matlab

I am working on a simulink model, in which i am getting a supply of a a variable at certain time intervals and i need to use the recent 10 values of that variable only.
how should i proceed with that?

It's a bit cheesy, but how about using a chain of Unit Delay blocks?

Related

Sampling time as an input-simulink

I am trying to make a library of functions that will allow me to parameterize filters and other function. In simulink standard blocks, I am only able to set a sampling time prior, rather than take an input. Is there any ideas on how I can create this? My first approach was to use conditionals and a clock to allow this parameter to exist, but the clock cannot be parameterized either.
Thanks in advance.
The only way to do this is to write each of your blocks as S-Functions.
If you are using m-code then within the setup method you'll need to define the block sample time as
block.SampleTimes = [-2 0];
then within the output method you'll need to set the next sample time (based on the value of the input signal) by assigning the new value into block.NextTimeHit.
An example of doing this can be found here: A Square Wave with Jitter
A similar thing can be done in a c-mex S-Function by using the mdlGetTimeOfNextVarHit method.

How to vary a specific variable during a SIMULINK simulation using Matlab programming

I am trying to vary a variable every 10 seconds while a simulation on simulink is running. I defined (Kb+Ks)/N inside some Gain blocks and I want to vary only Kb from its minimum to maximum value and back to its minimum value during simulation. I have tried using set_param(model, parameter, value) but it varies (Kb+Ks)/N instead of only Kb. I have also tried a 'for loop command,' however this runs the simulations one at a time. Please how do I solve this problem?
As suggested in the comments, you can't do what you want with a simple gain block. Replace Kb with a Repeating Sequence block, a From Workspace block, or whatever source signal you want to use. Then add that signal to a constant block Ks/N and multiply the output of the add block to whatever signal was previously going through the gain 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.

S-function storing system time in a variable

What am I trying to do is to save in a variable (global or constant) the system time. I am using a S-function in Simulink. The problem is that when I store the value of the system time in a variable it is continuously incrementing so when I do the difference between the current system time and the time stored in my variable is always 0. What do you think is the solution storing the system time in a variable and what type of variable should I use a global one or a constant. If you have any answer please give me an example because I am new in Matlab.
P.S I am using C language for the S-function.
It sounds as if you are trying to store the system time at the start of the simulation, then during the simulation compare the system time to that stored value. If so, then you should be using anR-Work vector to store the initial system time.
So in mdlInitializeSizes you want
ssSetNumRWork(S, 1);
Then in mdlStart you want
real_T *P_Tinit=ssGetRWork(S);
P_Tinit[0]=((real_T) clock())/CLOCKS_PER_SEC;
Then when you want to use the value use,
real_T itime;
itime=ssGetRWorkValue(S,0);
(The above assumes that you know how to actually get the system time, i.e. include the correct libraries, which from your question it sounds as if you do.)

control simulink from M-file

I am trying to control a simulink from a M-file.
What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.
I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?
Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.
I stuck in this problem for a very long time, could someone help me with this please?
Thank you very much.
You can get the current time of a running simulation with:
get_param('simulink_model_name', 'SimulationTime');
So for instance by checking this value from your M-file during simulation by using
timer(...)
you can detect when the simulation is at 0.6 seconds.
I used a combination of simulink and m-script to achieve a similar goal.
In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:
set_param(bdroot,'SimulationCommand','pause');
run('myscript.m'); %insert the script name
set_param(bdroot,'SimulationCommand','continue');
Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock).
The second inport is connected to constant block with a value of 0.6
On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.
The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.
Let me know if it worked.
This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?