How to do a memorizing counter?

2 次查看(过去 30 天)
Peter Daniel Mersch
回答: Keshav 2023-7-28
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 个评论
dpb
dpb 2023-6-24
编辑:dpb 2023-6-24
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.

请先登录,再进行评论。

回答(1 个)

Keshav
Keshav 2023-7-28
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.

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by