How to clear custom c-code variables from memory between simulation runs?

Hello
I have a Simulink model that has lots of custom c-code functions in it. It seems that in Simulink the custom code variables stay in the memory after the simulation has ended.
For example I have following code:
int i = 0;
int foo() {
i = i + 1;
return i;
}
The value of i does not reset to 0 between simulation runs. The value of i increments by 1 for everytime I run the simulation.
How do I unload the variables of custom c-code from the memory?
Thanks in advance.

Risposte (3)

You can use StartFcn and StopFcn callbacks in File->Model Properties->Model Properties->Callbacks to add and delete custom C code.

9 Commenti

Could you be more specific, how to add and delete custom C-code?
Firstly, can I see your model? Can you share it?
I made simple example to demostrate the problem. The value of output increases for every run during the session.
Change the filetype to .slx from mdl. For some reason .slx files are not supported here so I just changed the file format outside Matlab to allow file upload here.
This code should do it. Note that static variables will keep their values between each simulation.
The code:
int test()
{
int i=0;
i=i+1;
return i;
}
Oops, I got the wrong version of the example model. I intented to send example model with exact same code as in the starting post. The model will function in same way even when the variable is NOT static.
I'm trying to find a way to clear the value of i after the simulation has ended, but keep the value during the simulation run.
For example if I have a model, that has a variable called counter, that would keep track how many times certain conditions are met during simulation. I would like to keep the value of the variable between function calls, but not between different simulation runs. That is why I'm trying to find an easy way to clear the variables from MATLAB's memory, without closing the whole MATLAB.
The StopFcn callback in File->Model Properties->Model Properties->Callbacks to clear variables in the memory sounded very promising, but I could not find any MATLAB functions to clear the variables from custom code.
In my code, i is defined within the function and at every simulation, it is initialized to 0, equivalent to clearing it at the end of the simulation.
Yes, I can see that. But if the function test() is called 100 times during one simulation run, your code would always return 1.
What I want, is that if the function test() is called 100 times during one simulation run, it would return 1, 2, 3, ..., 99, 100.
The original code does excactly that. The problem is, when I run the simulation again, instead of starting from 0 (what I want), it starts counting where the previous simulation ended.
That is why I want to clear the variables after the simulation has ended, not every time I call the function.

Accedi per commentare.

Has this question be addressed? I am getting the same issue and need a fix.
Hello.
I was struggling a lot with this strange issue. Here is the workaround:
In m-function you'll should have the contruction of sort fo this:
persistent Is_First_Cycle;
if isempty(Is_First_Cycle)
Is_First_Cycle=1;
else
Is_First_Cycle=0;
end
Then you pass the flag to the to the C code:
coder.ceval('my_C_prg', Is_First_Cycle,...);
Then in C code you can add something like this:
//my_C_prg.c
void C_prg(double Is_First_Cycle, ...)
{
double var1;
double* refVar1;
double* refVar2;
if (Is_First_Cycle==1)
{var1=0;
VarsIni (&refVar1, &refVar2,...);
}
...
What moreover when i was trying to set pointer variable in main function for example:
void C_prg(double Is_First_Cycle,..., ..., ...)
{
double var1;
double* refVar1;
double* refVar2;
if (Is_First_Cycle==1)
{var1=0;
refVar1[0]=1;
refVar2[0]=3;
}
}
The all Matlab crashed. And funny thing is that, that when you I used the first construction (with inner VarsIni function), everything worked fine.
Best regards.

Categorie

Prodotti

Risposto:

il 12 Set 2022

Community Treasure Hunt

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

Start Hunting!

Translated by