writing a subclass of dynamicprops
allows to me to add properties dynamically to an object:
addprop(obj, 'new_prop')
this is great, but I would also love to create set / get
functions for these properties on the fly. Or analysis functions that work on these dynamic properties.
My experience with Matlab has been so far, that once I create an instance of a class, adding new methods is not possible. That is very cumbersome, because my object may contain a lot of data, which I'll have to re-load everytime that I want to add a new method (because I have to do clear classes
).
So is there a way to add methods on the fly?
I'm adding this answer because I think that this is not intuitive. At least not to myself at this moment. After finding this question I thought I had what I needed to be able to define the set/get methods for my dynamic class. All I wanted to achieve with this was something similar to what python does with its
__setattr__
method. In any case, here is a continuation of the class made by @jonas a while ago with a few modifications to add the our custom set method.With this class I'm defining the set method so that the parameter passed to the attribute is copied to the right type. To see what I mean consider the following usage:
With this we have forced the
x
attribute to be an integer of 8 bits which can only hold integers from -128 to 127. Also notice how the class of each attribute gives us the intended type.You cannot add methods like you add dynamic properties. However, there are two ways for implementing new methods during development that won't require you to re-load the data every time.
(1) I write standard methods as separate functions, and call them as
myMethod(obj)
during development. Once I'm sure they're stable, I add their signature into the class definition file - this requires aclear classes
, of course, but it is a much delayed one, and from time to time you may have to shut down Matlab, anyway.(2) With set/get methods, things are a little trickier. If you are using
dynamicprops
to add new properties, you can also specify their set/get methods, however (most likely, these methods/functions will want to receive the name of the property so that they know what to refer to):EDIT
(2.1) Here's an example of how to set up a hidden property to store and retrieve results (based on jmlopez' answer). Obviously this can be improved a lot if you have a better idea what you're actually designing