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

8 visualizaciones (últimos 30 días)
Caleb
Caleb el 17 de Nov. de 2022
Comentada: Walter Roberson el 17 de Nov. de 2022
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.

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 17 de Nov. de 2022
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 comentario
Walter Roberson
Walter Roberson el 17 de Nov. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by