Main Content

setPersistentVariables

Set persistent variables from MATLAB to the Simulink Real-Time target computer

Description

example

setPersistentVariables(target_object,variables_struct) sets persistent variables to values from MATLAB variables on the development computer into the persistent variables on the target computer. The variables can be empty or a struct whose fields are persistent variables.

Examples

collapse all

The getPersistentVariables function and setPersistentVariables function enables you to access persistent variables that are created and updated in the real-time application by using the Persistent Variable Read block and Persistent Variable Write block.

Get persistent variable values from target computer tg.

myPersist = getPersistentVariables(tg)
myPersist =

     []

Change the value of Variable1 in the myPersist structure. Add Variable1 to the structure.

myPersist.Variable1 = 10;
myPersist.Variable2 = int8([1, 2; 3, 4]);
myPersist
myPersist = 

  struct with fields:

    Variable1: 10
    Variable2: [2×2 int8]

Set the persistent variable values on the target computer. Get the variables from the target computer.

setPersistentVariables(tg,myPersist)
myMorePersist = getPersistentVariables(tg)
myMorePersist = 

  struct with fields:

    Variable1: 10
    Variable2: [2×2 int8]

You can use the setPersistentVariables function to remove the persistent variables that are stored on the target computer.

On the development computer, create a Target object tg and connect to the target computer.

tg = slrealtime;
connect(tg);

Use the setPersistentVariables function to clear the persistent variable values that are stored on the target computer.

setPersistentVariables(tg,[]);

You can use the setPersistentVariables function to remove a persistent variable that is stored on the target computer.

On the development computer, create a Target object tg and connect to the target computer.

tg = slrealtime;
connect(tg);

Get the persistent variable values from the target computer.

myPersistVars = getPersistentVariables(tg);

Copy the persistent variable values to a variable and remove the field for the variable. This example removes the field for the variable position.

myNewPersistVars = rmfield(myPersistVars,'position');

Use the setPersistentVariables function to apply the updated persistent variable values to the target computer.

setPersistentVariables(tg,myNewPersistVars);

Input Arguments

collapse all

Provides access to methods that manipulate the target computer properties.

Example: tg

A MATLAB struct whose field names are persistent variable names to set and whose field values are persistent variable values to set.

Example: myPersist

Version History

Introduced in R2022a