In Matlab, I can define a class as such:
classdef klass < handle
properties(Dependent)
prop
end
end
Matlab is perfectly happy instantiating an object of this class, even without defining a getter for prop
. It only fails when I try to access it (understandably). I'd like to set the GetMethod
dynamically based upon the property's name.
Unfortunately, even when the property is Dependent, the meta.property
field for GetMethod
is still read-only. And while inheriting from dynamicprops
could allow adding a property and programmatically setting its GetMethod in every instance, I don't believe it could be used to change an existing property. I may have to go this route, but as prop
must exist for every object I'd prefer to simply set the getter on a class-by-class basis. Is such a thing possible?
An alternative solution could be through some sort of catch-all method. In other languages, this could be accomplished through a Ruby-like method_missing
or a PHP-like __get()
. But as far as I know there's no (documented or otherwise) analog in Matlab.
(My use case: this class gets inherited by many user-defined subclasses, and all their dependent properties are accessed in a similar way, only changing based on the property name. Instead of asking users to write get.*
methods wrapping a call to the common code for each and every one of their dependent properties, I'd like to set them all dynamically with anonymous function pointers containing the necessary metadata).
Here is my proposal: create a method in the superclass called
add_dyn_prop
. This method is to be called in the subclasses instead of creating a dependent property the usual way.The idea is that the superclass inherit from
dynamicprops
and useaddprop
to add a new property, and set its accessor methods manually based on its name.now in the subclass, instead of defining a dependent property the usual way, we use this new inherited function in the constructor to define a dynamic property:
The output:
Of course now you can customize the getter method based on the property name as you initially intended.
EDIT:
Based on the comments, please find below a slight variation of the same technique discussed above.
The idea is to require the subclass to create a property (defined as abstract in the superclass) containing the names of the desired dynamic properties to be created. The constructor of the superclass would then create the specified dynamic properties, setting their accessor methods to generic functions (which could customize their behavior based on the property name as you requested). I am reusing the same
add_dyn_prop
function I mentioned before.In the subclass, we are simply required to implement the inherited abstract
dynamic_props
property, initialized with a list of names (or{}
if you dont want to create any dynamic property). For example we write:The superclass is similar to what we had before before, only now is it its responsibility to call the
add_dyn_prop
in its constructor for each of the property names:Note: I did not use
ConstructOnLoad
class attribute orTransient
property attribute, as I am still not sure how they would affect loading the object from a saved MAT-file in regards to dynamic properties.Check if this is what you want. The problem is that the user will need to get the properties using (), which may be quite boring, but anyway, I think this way you can change the variables. You can't change them directly on the class, but you can change the objects property values on demand. It doesn't need to change the values on the constructor, you can do that using another function that will be inherited by the classes.
klass1.m
klass2.m
Output: