Class for A = K * B in MATLAB (non-dependent properties with dependent behavior)

[Same question is asked here]
I want to implement a class for the function A = K * B, where all properties are non-dependent. However, I would like to change a property dynamically when one of the other properties changes.
I have started from a very simple class and ended up with the following. Is there an easier or more strong way (e.g. software pattern, use special set/get approaches etc..) to do this in MATLAB? (MATLAB gives lots of warnings in the setter functions)
classdef AeqKxB
properties
A
B
K
Kstate
end
properties (Access = private)
i % i=0 during construnction, i=1 normal operation
ia % ia = 1 to trick the setter function of B
ib % ib = 1 to trick the setter function of A
end
methods
function obj = MyClass2(a, k, kstate) % Constructor
obj.i = 0; % Construction starts
obj.A = a;
obj.B = obj.A*k;
obj.K = k;
obj.Kstate = kstate;
obj.i = 1; % Construction complete, ready for normal operation
obj.ia = 0; % Do not trick Set.B function for now
obj.ib = 0; % Do not trick Set.A function for now
end
function obj = set.A(obj, a) % setter for A
if obj.i == 0 % During construnction use normal operation
obj.A = a;
elseif obj.i == 1 % After construction
if obj.ia == 0 && obj.ib == 0
obj.A = a;
obj.ia = 1; %Get ready to Trick Set.B function
obj.B = obj.A / obj.K;
obj.ia = 0; %Trick finished, return to original
elseif obj.ia == 0 && obj.ib == 1
% Tricked Set.A function
obj.A = a;
end
end
end
function obj = set.B(obj, b) % setter for B
if obj.i == 0 % During construnction use normal operation
obj.B = b;
elseif obj.i == 1 % After construction
if obj.ia == 0 && obj.ib == 0;
obj.B = b;
obj.ib = 1; %Get ready to Trick Set.A function
obj.A = obj.B * obj.K;
obj.ib = 0; %Trick finished, return to original
elseif obj.ia == 1 && obj.ib == 0;
% Tricked Set.B function
obj.B = b;
end
end
end
function obj = set.K(obj,k) % setter for K
if obj.i == 0
obj.K = k;
elseif obj.i == 1
if strcmp(obj.Kstate, 'FixB')
obj.K = k;
obj.A = obj.B * obj.K;
elseif strcmp(obj.Kstate, 'FixA')
obj.K = k;
obj.B = obj.A / obj.K;
end
end
end
function obj = set.Kstate(obj,kstate)
obj.Kstate = kstate;
end
end
end

3 Comments

I want to implement a class for the function A = K * B, where all properties are non-dependent.
Why exactly are you trying to avoid using Dependent property attributes? This kind of thing is exactly what they're meant for. Trying to avoid them will probably cause problems.
When a property is declared as Dependent, its value cannot be assigned. In the code above, values of A and B both can be assigned although they depend on each other.
Let me put this way: Assume that we want to implement a function, y(x) for a given x. We can probably declare y as dependent and x as a non-dependent property. In this case, x has to be assigned, and y will be dependent on x. This is option A.
If we want to implement the same function as x(y) for a given y, then we have to declare x as dependent. This is option B.
We cannot implement both options A and B simultaneously (am I right?), and we have to select one of them. The code I provided has the flexibility of the implementation of both A and B simultaneously.
I hope there is an easier way to do this.

Sign in to comment.

 Accepted Answer

"where all properties are non-dependent" Why? Your properties are obviously dependent, so why not use them.
The warnings are telling you that if an object is saved in a mat file it may not be restored properly due to random initialisation order. In practice, with your design, that shouldn't be a problem as the properties' values should always be consistent, so I guess you could just disable the warnings.
However, I can't help but greatly dislike this design, particularly the i state variable. You could achieve a cleaner design, using dependent variables, with no impact on performance:
classdef AeqKxB
properties(Dependent)
A;
B;
K;
end
properties
Kstate;
end
properties (Access = private)
A_;
B_;
K_;
end
methods
function obj = AeqKxB(a, k, kstate) % Constructor
obj.A_ = a;
obj.B_ = a*k;
obj.K_ = k;
obj.Kstate = kstate;
end
function a = get.A(obj)
a = obj.A_;
end
function obj = set.A(obj, a) % setter for A
obj.A_ = a;
obj.B_ = a / obj.K_;
end
function b = get.B(obj)
b = obj.B_;
end
function obj = set.B(obj, b) % setter for B
obj.B_ = b;
obj.A_ = b * obj.K_;
end
function obj = set.K(obj,k) % setter for K
obj.K_ = k;
if strcmp(obj.Kstate, 'FixB')
obj.A_ = obj.B_ * k;
elseif strcmp(obj.Kstate, 'FixA')
obj.B_ = obj.A_ / k;
end
end
function obj = set.Kstate(obj,kstate)
obj.Kstate = kstate;
end
end
end

1 Comment

Ok. This is it. Many thanks!
Honestly speaking, this was the first approach I have taken, only to come to the false conclusion that it cannot be done. (I was getting 500 recursion error).
What I was missing was that, in the setter methods, say set.A, I was not assigning the following:
obj.A_ = a;
obj.B_ = a / obj.K;
So, herein, first we create full images of the dependent properties. We also create a connection between the images and the dependent properties through the set/get functions. When we make assignments on the image, it is reflected onto the dependent property.
Is there a name for this method? A any link for further reading?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!