user3489173
user3489173

Reputation: 45

sampling time counter in simulink

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.

Upvotes: 0

Views: 7402

Answers (1)

user2215426
user2215426

Reputation:

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

Upvotes: 1

Related Questions