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

classdependentMATLABobject oriented programmingset

[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

Best 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