MATLAB: I use persistent variables in a MATLAB Function Block, but do not see them declared as static in generated code? Why is this

ccodegensimulink coderstatestatesstatic

I use persistent variables in a MATLAB Function Block, but do not see them declared as static in generated code? Why is this?

Best Answer

You are correct in stating that persistent variables are not declared as static in generated code. But they are in fact declared as extern global variables by being outside any function calls, and the "extern" keyword added to their definition. This is a more general behavior than "static" as it is accessible by all modules, and displays the same memory persistence throughout the lifecycle of the program.
Now, every C file that wants to use a global variable must have "#include header.h" of the extern variable. Since persistent variables in MATLAB functions are to be used as "states" of the function itself, the notion of other files (and potentially other functions) accessing this data is not part of the persistent keyword.