Matt,
Thanks for your tip.
> > Is there any way to implement a static property in MATLAB classes?
>
> By a "static", you mean the same as a Constant property, except that it is changeable?
Yep, that's exactly what I'm talking about.
> You can fake it using persistent variables as in the classdef below.
That was my first workaround as well, but I found it not so elegant... in your example, manageVal is the function that is necessary but evil in my eyes.
My best take so far is to embed the Static property in an external handle class and share it among instances of the main class. This works (for my intended usage) but requires linking of the class objects. See below for my example.
I'm still exploring for other possibilities, so I'd appreciate any suggestions and feedbacks!
Kesh
%%% TWO CLASS IMPLEMENTATION of STATIC PROPERTY %%%
classdef mycounter < handle
properties
n = 0;
end
methods
function inc(obj)
obj.n = obj.n + 1;
end
end
end
%%%%%%%%%%%%%%%%%%
classdef myclass
properties
countobj;
end
properties (Dependent=true,SetAccess=private)
counter;
end
methods
function obj = myclass(c)
obj.countobj= c;
end
function inc(obj)
obj.countobj.inc();
end
function val = get.counter(obj)
val = obj.countobj.n;
end
end
end
%%%%%%%%%%%%%%%%%%
obj1 = myclass(mycounter);
obj2 = myclass(obj1.countobj);
obj2.counter
obj1.inc();
obj2.counter