From: Doug on
navels <navels(a)gmail.com> wrote in message <18915c18-6426-4cff-b66d-0c81291191df(a)x41g2000hsb.googlegroups.com>...
> Dave,
>
> Excellent, thanks for all the background info!
>
> Lee

This thread looks fairly stale, but it was the one that came up while I was searching, so I thought I would add my comment in the hope it will help others in the future.

I was also expecting to be able to have static properties, and had to find a work-around for what I was doing.

Here's a couple of things that I found one could do to get around this limitation.

As one option, the initial values don't have to be literals - you can call a function. See the "assigning values to constant properties" in the docs here: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/br4is5o-1.html

They use this example:

classdef NamedConst
properties(Constant)
RN = rand(5);
end
end

Unfortunately, this appears to only work with matlab intrinsic functions. In other words, I was unable to write my own function to do what I needed (not even a static one). Also, you can't change the value of RN at runtime.

In my case, the data member that I wanted to share across object instances was "kind of" "final". I didn't need to change the values at runtime, but I wanted all object instances to share a block of data. It was only created once, but that creation was computationally expensive, and the result was large. In other ways, the object is naturally a "value" object, but creating a copy of this "static" property for every instance was wasteful.

So here's the solution I used. I created a handle object that contained only the static property, then had a property in the main value object that contained a reference to the handle property. This way the static property could be shared (as a handle to an object) across instances, and only the handle was stored in the instances.

Note that this is, in effect, the Singleton pattern nicely shown here: http://www.mathworks.com.au/matlabcentral/fileexchange/24911

And reading the docs at the top of Simpleton.m, you see that they had to use the "persistent" keyword to work around the fact that "MATLAB OOP doesn't have the notion of static properties."

Anyway, here's the general mock up of what I did:

classdef myClassThatNeedsAStaticProperty
properties
referenceToHandleClass;
end
end

classdef HandleClassWithProperty < handle
properties
staticProperty;
end
end

This way I could create a single staticProperty (that was expensive to create and large) that was shared among instances of my value class.

After doing it this way, I realized that implementing it as a Singleton was actually a more elegant solution (in my situation) than a static property.

Hope this helps someone out there!