Main Content

Static Data

What Is Static Data

Static data refers to data that all objects of the class share and that you can modify after creation. Use static data to define counters used by class instances or other data that is shared among all objects of a class. Unlike instance data, static data does not vary from one object to another. MATLAB® provides several ways to define static data, depending on your requirements.

Static Variable

Classes can use a persistent variable to store static data. Define a static method or local function in which you create a persistent variable. The method or function provides access to this variable. Use this technique when you want to store one or two variables.

Saving an object of the class defining the persistent variable does not save the static data associated with the class. To save your static data in an object, or define more extensive data, use the static data object technique Static Data Object

Implementation

The StoreData class defines a static method that declares a persistent variable Var. The setgetVar method provides set and get access to the data in the persistent variable. Because the setgetVar method has public access, you can set and get the data stored in the persistent variable globally. Control the scope of access by setting the method Access attribute.

classdef StoreData
   methods (Static)
      function out = setgetVar(data)
         persistent Var;
         if nargin
            Var = data;
         end
         out = Var;
      end
   end
end

Set the value of the variable by calling setgetVar with an input argument. The method assigns the input value to the persistent variable:

StoreData.setgetVar(10);

Get the value of the variable by calling setgetVar with no input argument:

a = StoreData.setgetVar
a =

    10

Clear the persistent variable by calling clear on the StoreData class:

clear StoreData
a = StoreData.setgetVar
a =

     []

Add a method like setgetVar to any class in which you want the behavior of a static property.

Static Data Object

To store more extensive data, define a handle class with public properties. Assign an object of the class to a constant property of the class that uses the static data. This technique is useful when you want to:

  • Add more properties or methods that modify the data.

  • Save objects of the data class and reload the static data.

Implementation

The SharedData class is a handle class, which enables you to reference the same object data from multiple handle variables:

classdef SharedData < handle
   properties 
      Data1
      Data2
   end
end

The UseData class is the stub of a class that uses the data stored in the SharedData class. The UseData class stores the handle to a SharedData object in a constant property.

classdef UseData
   properties (Constant)
      Data = SharedData
   end
   % Class code here
end

The Data property contains the handle of the SharedData object. MATLAB constructs the SharedData object when loading the UseData class. All subsequently created instances of the UseData class refer to the same SharedData object.

To initialize the SharedData object properties, load the UseData class by referencing the constant property.

h = UseData.Data
h = 

  SharedData with properties:

    Data1: []
    Data2: []

Use the handle to the SharedData object to assign data to property values:

h.Data1 = 'MyData1';
h.Data2 = 'MyData2';

Each instance of the UseData class refers to the same handle object:

a1 = UseData;
a2 = UseData;

Reference the data using the object variable:

a1.Data.Data1
ans =

MyData1

Assign a new value to the properties in the SharedData object:

a1.Data.Data1 = rand(3);

All new and existing objects of the UseData class share the same SharedData object. a2 now has the rand(3) data that was assigned to a1 in the previous step:

a2.Data.Data1
ans =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575

To reinitialize the constant property, clear all instances of the UseData class and then clear the class:

clear a1 a2
clear UseData

Constant Data

To store constant values that do not change, assign the data to a constant property. All instances of the class share the same value for that property. Control the scope of access to constant properties by setting the property Access attribute.

The only way to change the value of a constant property is to change the class definition. Use constant properties like public final static fields in Java®.

See Also

|

Related Examples

More About