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.
