How to call a function within an other function in simulink with the S-Function-Builder, having static variables

Hello, i am trying to convert c-code to simulink models. Now i have a problem, which i couldn't solve yet. Hope you can help me. I have the following problem: thera are for example three functions in C; f1.c, f2.c and counter.c.
int counter (void) { static int count; count=count+1; return count; } (<=Block1)
int f1(int a) {int c=counter(); int b=c+a; return b; } (<=Block2)
int f2(int a) {int c=counter(); int d=c-a; return d; } (<=Block3)
I am able to generate blocks from this code, my only problem is, how can I get the variable count stay static in Simulink? The generated blocks dont have the same value for count in the simulation. Although there is only one source code in C for counter. Thank you in advance!!!

2 comentarios

Have you created 3 blocks from single C file ?
No, there are 3 separate c-files, but two of them (f1.c and f2.c) call the c-file with the function int counter(), which contains the static variable. I ment, that there is only one surce file for the function counter().

Iniciar sesión para comentar.

 Respuesta aceptada

Since f1.c and f2.c are separate S-functions (shared library files underneath), my guess is that counter.c is treated as a separate translation unit for each library. What happens if you create a separate library out of counter.c and link against that library from f1.c and f2.c?
Also, note that static variable don't work very well with Simulink in general. See Why are static variables in C or FORTRAN MEX S-functions not reset when running successive simulations?.

Más respuestas (0)

Categorías

Más información sobre Simulink Coder en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 17 de Sept. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by