Is there a way to define static member variables in MATLAB classes?
This doesn't work:
classdef A
properties ( Static )
m = 0;
end
end
It suggests to use keyword "Constant" instead of "Static", the constant properties cannot be modified. I want a variable common to all objects of class A
and I want to be able to modify that variable in methods of class A
.
So what I need is a private static member variable. Is there a way to obtain it in MATLAB?
Found out that a workaround can be done using persistent variables in static member functions.
In this case you should inherit all your classes from a base class like the following.
classdef object < handle
properties ( GetAccess = 'public', SetAccess = 'private' )
id
end
methods ( Access = 'protected' )
function obj = object()
obj.id = object.increment();
end
end
methods ( Static, Access = 'private' )
function result = increment()
persistent stamp;
if isempty( stamp )
stamp = 0;
end
stamp = stamp + uint32(1);
result = stamp;
end
end
end
(just to inform) there is (another?) way to create static-like data in matlab
suppose that you have a "handle" class which its name is "car" if you want the car class to have static data, you could construct another handle class and use it in car class throw composition, the latter class works as a static data for car class
this way when you create first instance of a car class, an instance of STATIC_DATA_HOLDER will be created and when you create second instance of car class it uses previously created STATIC_DATA_HOLDER class.
these code tested with "MATLAB 2013b"
Here's a direct way to create a static property in Matlab. The only difference between this implementation and a hypothetical (but impossible; see Mikhail's answer) true static property is the syntax for setting the member variable.
Now the static property staticVar can be read via:
...and be set via:
So, for instance, this is the expected output from a test of this functionality:
This approach works just as well for private static properties like you requested, but the demo code is a little longer. Note that this is not a handle class (though it would work perfectly well on a handle class as well).
...and the test:
Another workaround to get something like static properties is to use the fact that initialisation code for member variables is only executed once when the class file is loaded. That means, if you have a definition like
then
some_function
is invoked only once, and if it returns an object of class type, this will be shared by all instances. I've added a sample implementation that shows how that can be used:If you run this sample code
you'll see, that
classvars
is indeed shared. I think this solution is much nicer than using persistent variables in functions, since you can reuse theStaticVarContainer
as often as you want, it's easier to use, and furthermore, you directly see the initialisation of the static variables in the properties section.To get the result, that is desired in the OP's question (i.e. implementing an object counter) the shared property can be made
Constant
, so that it can be referenced without an instance at hand:Note, that the
Constant
attribute only means that, e.g.obj1.static
cannot be changed, but it does not affectobj1.static.counter
which is not constant, and can be set to heart's desire.You can not, it is by design. You should use a
persistent
variable (technique from the MATLAB as 1980 applied in year 2011)!For completeness I should mention that actually there is as of 2010b an undocumented and probably not longer supported
static
property modifier.For background see here the answer of Dave Foti, MATLAB OO group manager: