Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OOP: Static Class Property?

489 views
Skip to first unread message

Kesh Ikuma

unread,
Nov 10, 2011, 10:55:14 AM11/10/11
to
Is there any way to implement a static property in MATLAB classes?

When I tried Static attribute for property, the "red-wiggle" told me to use the Constant attribute. But, when I set Constant=true, its value (obviously) cannot be changed once set.

Here's a quick example:

classdef myclass
properties (Constant)
val = 0;
end
methods (Static)
function increment()
myclass.val= myclass.val + 1;
end
end
end

obj = myclass % shows val: 0
myclass.increment
obj % val is still 0

Thanks,
Kesh

Matt J

unread,
Nov 10, 2011, 2:06:10 PM11/10/11
to
"Kesh Ikuma" <tik...@hotmail.removethis.com> wrote in message <j9gs52$pb7$1...@newscl01ah.mathworks.com>...
>
> 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? You can fake it using persistent variables as in the classdef below. Example usage:

>> myClass.setval(3); myClass.getval

ans =

3

>> myClass.increment; myClass.getval

ans =

4




%%%%Everything below is inside myClass.m
classdef myClass


methods (Static)



function obj=setval(newval)

manageVal('set',newval);

end

function out=getval

out=manageVal('get');

end

function increment

manageVal('increment');

end

end




end

function outval=manageVal(operation,newval)

persistent val; %A fake static property

switch operation

case 'set'

val=newval;

case 'increment'

val=val+1;

end

outval=val;

end

Kesh Ikuma

unread,
Nov 10, 2011, 3:03:12 PM11/10/11
to
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

Matt J

unread,
Nov 11, 2011, 10:11:29 AM11/11/11
to
"Kesh Ikuma" <tik...@hotmail.removethis.com> wrote in message <j9ham0$gog$1...@newscl01ah.mathworks.com>...
>
> 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.
===============

I don't have a clear idea of what constitutes "evil" or "inelegant" in your mind, but going by your example, the solution seems to be a hybrid of the two methods: make the mycounter handle a persistent variable inside the myclass constructor. In the example below, I've done this in a way that gets rid of the increment() method. All incrementation is handled automatically inside the constructor. But if this isn't what you want, it's easily modified...

classdef mycounter < handle
properties
n = 1;
end
end
%%%%%%%%%%%%%%%%
classdef myclass
properties
countobj;
end
properties (Dependent=true,SetAccess=private)
counter;
end
methods
function obj = myclass

persistent c

if isempty(c)
c=mycounter;
else
c.n=c.n+1;
end

obj.countobj= c;

end

function val = get.counter(obj)
val = obj.countobj.n;
end
end
end
%%%%%%%%

Example Usage:


>> obj1=myclass; obj1.counter

ans =

1

>> obj2=myclass; obj2.counter, obj1.counter

ans =

2


ans =

2

Philip Borghesani

unread,
Nov 11, 2011, 10:40:08 AM11/11/11
to Kesh Ikuma
On 11/10/2011 3:03 PM, Kesh Ikuma wrote:
Kesh,
Given your implementation of mycounter myclass can be simper: initialize
the property countobj when it is declared.

> classdef mycounter < handle
> properties
> n = 0;
> end
> methods
> function inc(obj)
> obj.n = obj.n + 1;
> end
> end
> end
> %%%%%%%%%%%%%%%%%%
> classdef myclass
> properties
countobj=mycounter; %only one handle object is ever created until %
clear classes is called
> end
> properties (Dependent=true,SetAccess=private)
> counter;
> end
> methods
> function obj = myclass()

Kesh Ikuma

unread,
Nov 11, 2011, 10:39:29 AM11/11/11
to
Matt,

> I don't have a clear idea of what constitutes "evil" or "inelegant" in your mind, but

Often (in MATLAB at least) I use classes to avoid these interpreter functions (i.e., ones that take a command and its arguments). So, using an interpreter function like manageVal() within a class defeats my original intention of introducing the class.

I know, it's highly personal preference and a pet-peeve of mine, and nothing against others doing so though. :)

In any case, I really like how you use the persistent in your latest take.

'preciate your insights!

Best,
Kesh


"Matt J" wrote in message <j9jdv1$cif$1...@newscl01ah.mathworks.com>...
0 new messages