Basic implementation of events and listeners.
이전 댓글 표시
I am trying to write a class with all properties depending on eachother It seems events and listeners can be used for this, but I can't get the basic implementation of these to work.
% when property a or b is altered, sum will automatically be recalculated
classdef myclass < handle
properties
a = 1;
b = 2;
sum
end
events
valuechange
end
methods
function obj = myclass()
addlistener(obj, valuechange, calc_sum(obj))
end
function obj = set.a(obj, input)
obj.a = input;
notify(obj, valuechange)
end
function obj = set.b(obj, input)
obj.b = input;
notify(obj, valuechange)
end
function calc_sum(obj)
obj.sum = obj.a + obj.b
end
end
end
an attempt to create an object returns following error:
Undefined function or variable 'valuechange'.
Error in myclass (line 18)
addlistener(obj, valuechange, calc_sum(obj))
What am I not understanding?
Thanks
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Physical Units에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!