Persistent variables enable you to model registers. If you need to preserve state between invocations of your MATLAB® algorithm, use persistent variables.
Before you use a persistent variable, you must initialize it with a statement specifying its size and type. You can initialize a persistent variable with either a constant value or a variable, as in the following examples:
% Initialize with a constant persistent p; if isempty(p) p = fi(0,0,8,0); end
% Initialize with a variable initval = fi(0,0,8,0); persistent p; if isempty(p) p = initval; end
Use a logical expression that evaluates to a constant to test whether a persistent variable has been initialized, as in the preceding examples. Using a logical expression that evaluates to a constant ensures that the generated HDL code for the test is executed only once, as part of the reset process.
You can initialize multiple variables within a single logical expression, as in the following example:
% Initialize with variables initval1 = fi(0,0,8,0); initval2 = fi(0,0,7,0); persistent p; if isempty(p) x = initval1; y = initval2; end
Note: If persistent variables are not initialized as described above, extra sentinel variables can appear in the generated code. These sentinel variables can translate to inefficient hardware. |