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 件のコメント

TAB
TAB 2012 年 9 月 17 日
Have you created 3 blocks from single C file ?
Maxim
Maxim 2012 年 9 月 17 日
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().

サインインしてコメントする。

 採用された回答

Kaustubha Govind
Kaustubha Govind 2012 年 9 月 17 日
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?.

1 件のコメント

Maxim
Maxim 2012 年 9 月 18 日
Thank you for the very fast answer. I'll try it with a separate library.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by