1
作为MATLAB新手,我正在尝试编写一个类,如果两个属性中的一个更改了值,则会自动重新计算第三个属性。自动更新的Matlab属性
看来事件和听众是为此而做的,但我无法得到他们基本实现的窍门。
我的最新尝试是这样的
% when property a or b is altered, c will automatically be recalculated
classdef myclass < handle
properties
a = 1;
b = 2;
c
end
events
valuechange
end
methods
function obj = myclass()
addlistener(obj,'valuechange', obj.calc_c(obj))
end
function set_a(obj, input)
obj.a = input;
notify(obj, valuechange)
end
function set_b(obj, input)
obj.b = input;
notify(obj, valuechange)
end
function calc_c(obj)
obj.c = obj.a + obj.b
end
end
end
它返回以下错误
Error using myclass/calc_c
Too many output arguments.
Error in myclass (line 18)
addlistener(obj,'valuechange', obj.calc_c(obj))
我在做什么错?
看来我要找的是这里所描述的功能:http://stackoverflow.com/questions/8098935/matlab-dependent-properties-and-calculation?rq= 1 – user1361521
我也在这里发布了一个监听器/观察器的例子:http://stackoverflow.com/questions/9153044/is-it-possible-to-do-stateless-programming-in-matlab-how-to-avoid-checking- d/9153153#9153153 – bdecaf