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.
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
StoreData class defines a static method
that declares a persistent variable
provides set and get access to the data in the persistent variable.
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
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
an input argument. The method assigns the input value to the persistent
Get the value of the variable by calling
no input argument:
a = StoreData.setgetVar
Clear the persistent variable by calling
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.
SharedData class is a handle class, which
enables you to reference the same object data from multiple handle
classdef SharedData < handle properties Data1 Data2 end end
UseData class is the stub of a class
that uses the data stored in the
UseData class stores the handle to a
in a constant property.
classdef UseData properties (Constant) Data = SharedData end % Class code here end
Data property contains the handle of
SharedData object. MATLAB constructs the
when loading the
UseData class. All subsequently
created instances of the
UseData class refer to
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:
ans = MyData1
Assign a new value to the properties in the
a1.Data.Data1 = rand(3);
All new and existing objects of the
share the same
rand(3) data that was assigned to
the previous step:
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
UseData class and then clear the class:
clear a1 a2 clear UseData
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
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®.