Is there a way to make a persistent variable using the C Caller block?

조회 수: 11 (최근 30일)
Caleb
Caleb 2022년 11월 17일
댓글: Walter Roberson 2022년 11월 17일
Is there a way to use the C Caller block in Simulink to access a variable from a previous call to the block?
For example, I would want to create a struct the first time the function is called and initialize variables inside the struct with values read from a file and in subsequent calls to the function, I would simply like to access that struct.
I know if I use a matlab function block, I can use the persistent keyword, but I am not sure if there is an equivalent uisng the C Caller.
I think all I would really need is a way to pass a pointer to the struct between function calls.
  댓글 수: 1
Jim Riggs
Jim Riggs 2022년 11월 17일
The equivalent to a Matlab "persistent" variable in C is a "static variable".

댓글을 달려면 로그인하십시오.

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 11월 17일
Your MATLAB release is R2018b so I assume you are referring the "C Caller" block, not the "C Function" block.
"C Caller" block is used to call an existing C function. You may or may not have source control of the C function.
The C function may have a static (persistent) variable, but if it is not on the C function interface, then you don't have access to it.
As long as you have access to a variable inside the C function, you could use Memory/Read/Writer block to make it "persistent" in Simulink, even though it is not "persisent" in the C function.
"C Function" block is to write C code in Simulink. You can do whatever you want. It's available in R2020a.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 11월 17일
If it is an automatic variable (which most variables are in C) then you cannot access it outside of the block that creates the variable, except by passing its value or a pointer to it into another routine from inside of the block. Returning a pointer to such a variable out of the declaring block would lead to problems, as the memory is considered to be freed after the end of the block.
If it is a static variable (possible in C) then you still cannot access it outside of the declaring block, except by passing it or a pointer to it. You "shouldn't" return a pointer to such a variable, but doing so might work (no promises, the interface contract does not promise access from outside of the file.)
If it is an extern variable, then Yes, you can pass around pointers to it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by