Class for A = K * B in MATLAB (non-dependent properties with dependent behavior)
Show older comments
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.
Baris Erkus
on 8 Jan 2016
Edited: Baris Erkus
on 8 Jan 2016
Baris Erkus
on 8 Jan 2016
Accepted Answer
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!