필터 지우기
필터 지우기

initialize persistent variables using Matlab coder

조회 수: 15 (최근 30일)
Ian Cassimatis
Ian Cassimatis 2024년 4월 16일
답변: Poorna 2024년 5월 3일
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?

답변 (1개)

Poorna
Poorna 2024년 5월 3일
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!

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by