This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Initialize Persistent Variables in MATLAB Functions

A persistent variable is a local variable in a MATLAB® function that retains its value in memory between calls to the function. For code generation, functions must initialize a persistent variable if it is empty. For more information, see persistent.

When programming MATLAB functions in these situations:

the specialized semantics impact how a function initializes its persistent data. Because the initialization must be independent of the input to the function, follow these guidelines:

  • The function initializes its persistent variables only by accessing constants.

  • The control flow of the function does not depend on whether the initialization occurs.

For example, this function has a persistent variable n.

function y = fcn(u)
    persistent n
        
    if isempty(n)
        n = u;
        y = 1;
        return
    end
    
    y = n;
    n = n + u; 
end
This type of initialization results in an error because the initial value of n depends on the input u and the return statement interrupts the normal control flow of the function.

To correct the error, initialize the persistent variable by setting it to a constant value and remove the return statement. For example, this function initializes the persistent variable without producing an error.

function y = fcn(u)
    persistent n
        
    if isempty(n)
        n = 1;
    end
    
    y = n;
    n = n + u; 
end

MATLAB Function Block With No Direct Feedthrough

This model contains a MATLAB function block that defines the function fcn, described previously. The input u is a square wave with values of 1 and -1.

In the MATLAB function block:

  • The initial value of the persistent variable n depends on the input u.

  • The return statement interrupts the normal control flow of the function.

Because the Allow direct feeedthrough check box is cleared, the initialization results in an error.

If you modify the function so it initializes n independently of the input, then you can simulate an error-free model.

State Control Block in Synchronous Mode

This model contains a MATLAB function block that defines the function fcn, described previously. The input u is a square wave with values of 1 and -1.

In the MATLAB function block:

  • The initial value of the persistent variable n depends on the input u.

  • The return statement interrupts the normal control flow of the function.

Because the model contains a State Control block in Synchronous mode, the initialization results in an error.

If you modify the function so it initializes n independently of the input, then you can simulate an error-free model.

Stateflow Chart Implementing Moore Semantics

This model contains a Stateflow chart that implements Moore machine semantics. The chart contains a MATLAB function that defines the function fcn, described previously. The input u has values of 1 and -1 that depend on the state of the chart.

In the MATLAB function:

  • The initial value of the persistent variable n depends on the input u.

  • The return statement interrupts the normal control flow of the function.

Because the chart implements Moore semantics, the initialization results in an error.

If you modify the function so it initializes n independently of the input, then you can simulate an error-free model.

See Also

| | |

Related Topics