How to do a memorizing counter?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1418663/image.png)
1 commentaire
dpb
le 24 Juin 2023
Modifié(e) : dpb
le 24 Juin 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.
Réponses (1)
Keshav
le 28 Juil 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!