How to clear custom c-code variables from memory between simulation runs?
조회 수: 5 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (3개)
Birdman
2018년 3월 28일
You can use StartFcn and StopFcn callbacks in File->Model Properties->Model Properties->Callbacks to add and delete custom C code.
댓글 수: 9
Birdman
2018년 3월 28일
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.
Adam Johnson
2022년 2월 2일
Has this question be addressed? I am getting the same issue and need a fix.
댓글 수: 0
Pawel Hanczur
2022년 9월 12일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!