Clear Filters
Clear Filters

How to do a memorizing counter?

2 views (last 30 days)
Peter Daniel Mersch
Peter Daniel Mersch on 24 Jun 2023
Answered: Keshav on 28 Jul 2023
Hello everyone,
i try to build a counter which can save the value of a counter from the first to the second simulation. (I use Matlab 2023a)
For example the magzin is filled by one in the first Simulation so there is one in there. In the Second Simulation its filled by one again so there are two in there.
To make this possible i tried to creat a counter using a "To Workspace" to write the counter value at the end of the Simulation in my Workspace (that works so far. After the first Simulation the Variable Magazin2 has the value of 1) and i use a constant block to put the current counter value back into the simulation.
It doesnt Work as intented. After the first Simulation it has the value of one, which is correct, but at the second simualtion it's still only one.
In the following i will add what i programmed.
I am grateful for every response. Thanks
  1 Comment
dpb
dpb on 24 Jun 2023
Edited: dpb on 24 Jun 2023
You didn't show us the MATLAB function and my old eyes can't read the small graphic and I don't know anything about Simulink anyway, but I'm guessing you're looking for/needing the persistent keyword to retain the value of a function variable between calls.
Using one then requires you build a way to reset it when needed, too, otherwise it is never cleared/reset.
Although I don't know about the lifetime of the connection with Simulink and how you intend this to work; it may be that the function space is cleared between invocations of the Simulink model; if so, that will clear the persistent variable. If that is a problem, you may have to revert to using a disk file as the storage/persistent variable.

Sign in to comment.

Answers (1)

Keshav
Keshav on 28 Jul 2023
For debugging purposes, try displaying the port value while simulating your model to check where the value is going wrong.
You can refer to the below MathWorks documentation to know how to display port value on Simulink models.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!