0

how can I take simulink time from "clock" block in simulink and then increase a counter (k) in a matlab embedded function at every sampling period Ts?

let us say if simulink clock time is t, then we may consider

if mod(t, Ts)==0
   k=k+1;
end

but this will not work since simulink time is variable. Any idea? Thanks.

user3489173
  • 45
  • 1
  • 9

1 Answers1

1

I'm assuming that you are only interested in doing this for variable step solvers. Let us assume that your sampling time is every two seconds, and your solver is ode23t, with the simulation running for ten seconds. Then, you expect to have the value of your variable be 5.

Now, the way you set up the model is in the screenshot below. Constant is your sampling time, Clock is the source for the time. You need a Rate Transition block to ensure a periodic output for a non-periodic clock input into it. I've set a sampel time of 1 for it.

Finally, in your code, you need a persistent variable. You could also use Simulink's Data Store Read and Write blocks, but this seems simpler to me.

enter image description here

  • thanks for the solution you provided. One question: why did you set output port sample time of Rate Transition block to 1? – user3489173 Aug 15 '14 at 17:02
  • That could be whatever you want it to be. The 1 means 1 second. And that seems like a nice even number for time measurements. However, you could set it to 0.5 seconds, or even smaller. Note that that will affect how often the clock signal input into the MATLAB Function block is updated. –  Aug 15 '14 at 19:00
  • thanks. i understood now but this did not solve what I really want to do. Your answer is correct, however. Do you have an e-mail to explain you the problem? – user3489173 Aug 15 '14 at 19:05
  • my real problem is as follow. I have a continuous system and I want to control this system by a discrete model predictive controller. The model predictive controller inside the embedded matlab function will create a NEW control input at every sampling time. Otherwise, it will apply the OLD control input calculated at the previous sampling time continuously at other time instants. As I result, I want to determine from the clock time when a SAMPLING INSTANT occurs to update the control input.. – user3489173 Aug 15 '14 at 19:19
  • as a result, my new question boils down to: from clock time in simulik, how can we understand that a sampling instant occurred? As I wrote at the beginning of my question, if i use "mod(t, Ts)==0", this will not work. I need something like this to detect a sampling instant from continuous clock time. – user3489173 Aug 15 '14 at 19:34
  • the solution is here: http://www.mathworks.com/matlabcentral/answers/42594-embedded-matlab-function-block-sample-time – user3489173 Aug 16 '14 at 08:58
  • Nice that you were able to find the complete answer. Also, I think you'd be better served just to create a new question as an extension of an existing question if you have further discussions to the answer –  Aug 17 '14 at 20:24