The following class cannot be initialized, as any call to the constructor gives the error:
>> MyClass('a',2)
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available
stack space can crash MATLAB and/or your computer.
Error in MyClass/MyClass/@(o,p)setProp(o,propname,p)
classdef MyClass < dynamicprops methods function obj = MyClass(propname, val) P = obj.addprop(propname); P.SetMethod = @(o,p)setProp(o,propname,p); obj.(propname) = val; end function setProp(obj, propname, val) % do other stuff, like evaluating if val is legitimate
obj.(propname) = val; end endend
Why is this, and what is the best way to write custom 'set' methods for dynamic properties with unknown names?
Best Answer