Generate Code for Persistent Variables
This example shows how to generate a MEX function from a MATLAB® function, compute_average
, that uses persistent variables. It illustrates that you must clear the state of persistent variables before using the function to compute the average of a new set of values.
This example also shows how to initialize and terminate the state of the persistent variables for the same MATLAB function in standalone generated code. You must clear the state of persistent variables in the generated code before using the function to compute the average of a new set of values.
Prerequisites
There are no prerequisites for this example.
About the compute_average
Function
The compute_average.m
function uses two persistent variables, the accumulated sum and the number of values added so far, so that you can call the function with one value at a time.
type compute_average
% y = compute_average(x) % This function takes an input scalar value 'x' and returns the average % value so far. function y = compute_average(x) %#codegen assert(isa(x,'double')); % Input is scalar double % Declare two persistent variables 'sum' and 'cnt'. persistent sum cnt; % Upon the first call we need to initialize the variables. if isempty(sum) sum = 0; cnt = 0; end % Compute the accumulated sum and the number of values so far. sum = sum + x; cnt = cnt + 1; % Return the current average. y = sum / cnt;
The %#codegen
directive indicates that the MATLAB code is intended for code generation.
Generate the MEX Function
First, generate a MEX function using the command codegen
followed by the name of the MATLAB file to compile. Specify the type of the input argument to be a scalar double.
codegen compute_average -args 0
Code generation successful.
By default, codegen
generates a MEX function named compute_average_mex
in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.
Run the MEX Function
(10 + 20 + 100) / 3 = 43.3333
compute_average_mex(10)
ans = 10
compute_average_mex(20)
ans = 15
compute_average_mex(100)
ans = 43.3333
Clear the Internal State of Persistent Variables
Clear the persistent variables by using the clear mex
command.
clear mex
Run the MEX Function Again to Calculate the Average of a Different Set of Values
(10 + 20 + 30 + 40) / 4 = 25
compute_average_mex(10)
ans = 10
compute_average_mex(20)
ans = 15
compute_average_mex(30)
ans = 20
compute_average_mex(40)
ans = 25
Clear the Internal State of Persistent Variables in Standalone Generated Code
The states of persistent variables in standalone generated code are cleared by calling the initialize and terminate functions in the main function. These functions are generated by the code generator. These files are in the codegen
directory.
You can edit the example main file, main.c
to invoke the initialize and terminate functions. For example:
type main.c
/* * File: main.c */ /* Include Files */ #include "main.h" #include "compute_average.h" #include "compute_average_terminate.h" #include "compute_average_initialize.h" /* Function Declarations */ static double argInit_real_T(void); static void main_compute_average(void); /* Function Definitions */ /* * Arguments : void * Return Type : double */ static double argInit_real_T(void) { return 0.0; } /* * Arguments : void * Return Type : void */ static void main_compute_average(void) { double y; /* Initialize function 'compute_average' input arguments. */ /* Call the entry-point 'compute_average'. */ y = compute_average(argInit_real_T()); } /* * Arguments : int argc * const char * const argv[] * Return Type : int */ int main(int argc, const char * const argv[]) { (void)argc; (void)argv; /* Initialize the entry-point function. */ compute_average_initialize(); /* Invoke the entry-point functions. You can call entry-point functions multiple times. */ main_compute_average(); /* Terminate the application. */ compute_average_terminate(); /*Once the application is terminated, the state of the persistent variables is cleared. */ /* Re-initialize the entry-point function. */ compute_average_initialize(); /* You can run the application for a new set of values.*/ main_compute_average(); /* Terminate the application after your process is complete.*/ compute_average_terminate(); return 0; } /* * File trailer for main.c * * [EOF] */
As you can see, the main.c
file has been edited to call the terminate function, compute_average_terminate()
to clear the state of the persistent variables. A new set of computations is run by calling compute_average_initialize()
and main_compute_average()
with a new set of values.