initialize persistent variables using Matlab coder

9 views (last 30 days)
My Matlab code utilizes a few persistent variables and currently uses "isempty" for one of the variables to ensure all are intialized before use. The resulting Matlab coder initialization function sets a bool "var_isempty". This seems rather clunky. To me it seems that the appropriate coder output should instead initialize the values in the static variable definition within the function.
Is there a codegen option to generate the code in this manner? Or another way?
The https://www.mathworks.com/help/coder/ug/use-generated-initialize-and-terminate-functions.html refers to both global and persistent variables witht the -global option. Is this the best method?

Answers (1)

Poorna
Poorna on 3 May 2024
Hi Ian,
I see you want the initilialize persistent variables which get translated to static variables in 'C' right in the definition of the variable in the code. As fas as I know there is no option to generate code to define a static variable within the declaration as of R2024a. But it is to be understood that although using a bool variable to check if the static variable is initialized or not may seem to be a little inefficient, it helps in initializing the static variables in a much more wider cases. Consider the case where the static variable is initialized after doing some computation instead of just assigning it a constant value. Having a separate function would help for you to put all your initialization computation in there separately which cannot be done in the declaration.
Hope this Helps!

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!