class setter method - how to handle initialization and update?
6 views (last 30 days)
Show older comments
Hello,
I implemented a Matlab class with various properties and an update function that processes these properties. To improve usability, I want the update function to be called as soon as a parameter is changed using the set method.
classdef ClassA
function set.par1(obj,val)
obj.par1 = val;
obj.update;
end
% --> ClassA.par1 = x changes par1 and calls ClassA.update;
However, during the constructor of the class instance the update function should not be called. The reason for this is that not all parameters are set yet, so calling update() with illegal parameters results in an error and I don't want to use default parameters because this would cause other problems (such as excecution time of the constructor and a lot more in the proceeding code). In my implementation, I used another property "init" that is set and reset during the constructor and checked in each setter method.
function set.par1(obj,val)
obj.par1 = val;
if obj.init == 0
obj.update;
end
end
It works as desired, but it leads to a warning of the code analyzer "the set method of par1 should not access another property".
Question 1: Is there a more elegant way to do it and therefore to avoid the code analyzer warning? Such as specifying NOT to call the setter method during the constructor when setting a parameter in the constructor code? Or is my "init parameter workaround" the only way?
The next issue arises when I have another, different Matlab class that also has an update function, but during the update function of the new class I need to get properties of the previously mentioned class. For the same usability reason, I want to perform update() in classB as soon as a parameter in classA changes. My current implementation does not work, because when I call the classB update function in the setter function of classA, the respective get value is still the old value. I have tried it with an event-listener-PostSet-approach but since both my classes are not derived from handle this appears to be impossible (code analyzer error). In the example, ClassC is a handle class that contains both ClassA and ClassB instances.
classdef ClassA
properties
par1
init
myClassC % This is derived from handle class
end
function set.par1(obj,val)
obj.par1 = val;
if obj.init == 0
obj.update;
obj.myClassC.ClassB.update;
end
end
classdef ClassB % ClassB also has a myClassC handle property
function obj.update
xyz = obj.myClassC.ClassA.par1; % the getter function of ClassA Returns the "old" value
... % further calculations
end
end
Question 2: How can I call the update function of another class instance (ClassB) when one Parameter of a different class (ClassA) is changed? The classes are "linked" through a handle-derived class (ClassC) that contains both ClassB and ClassA properties but since the Parameter Change occurs in ClassB, an event-listener implementation is imo not possible?
Thanks a lot!
0 Comments
Answers (0)
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!