callback within the class?

4 ビュー (過去 30 日間)
jeff wu
jeff wu 2012 年 3 月 13 日
hi, i got the following class:
classdef form<handle
properties
type = 'square'
b
h
A
Iy
Iz
Wy
Wz
end %properties
events
calc
end %events
methods
function f = form(b,h)
f.b=b;
f.h=h;
f.update()
end
function obj = update(obj)
obj.A= obj.h*obj.b;
obj.Iy= obj.h^3*obj.b/12;
obj.Iz= obj.b^3*obj.h/12;
obj.Wy= obj.h^2*obj.b/6;
obj.Wz= obj.b^2*obj.h/6;
end
end % events
end%class
how do i manage to call the function update everytime when either form.a or form.h of an instance is changed withoud adding a listener to the instance? is there a way to directly built thin in the class definition?
thanks a lot for help

採用された回答

Daniel Shub
Daniel Shub 2012 年 3 月 14 日
It seems like all the properties other than b and h should be dependent. This would allow you to get rid of the update method. If you really want to go the update route, you could write a set method for b and h which will call the update method.
Does the following work for you?
classdef form<handle
properties
type = 'square'
b
h
end
properties (Dependent)
A
Iy
Iz
Wy
Wz
end %properties
methods
function f = form(b,h)
f.b=b;
f.h=h;
end
function val = get.A(obj)
val= obj.h*obj.b;
end
function val = get.Iy(obj)
val= obj.h^3*obj.b/12;
end
function val = get.Iz(obj)
val= obj.b^3*obj.h/12;
end
function val = get.Wy(obj)
val= obj.h^2*obj.b/6;
end
function val = get.Wz(obj)
val = obj.b^2*obj.h/6;
end
end % events
end%class
You could also go with the "ugly" update method:
classdef form<handle
properties
type = 'square'
b
h
A
Iy
Iz
Wy
Wz
end %properties
methods
function f = form(b,h)
f.b=b;
f.h=h;
end
function set.b(obj, val)
obj.b = val;
obj.update;
end
function set.h(obj, val)
obj.h = val;
obj.update;
end
function obj = update(obj)
obj.A= obj.h*obj.b;
obj.Iy= obj.h^3*obj.b/12;
obj.Iz= obj.b^3*obj.h/12;
obj.Wy= obj.h^2*obj.b/6;
obj.Wz= obj.b^2*obj.h/6;
end
end % events
end%class

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 3 月 14 日
You do not appear to have a form.a . You do have a form.A which is overwritten in update() . If update() is to be called as a callback when form.A is changed, then it would be called when form.A is changed within update()
What is the point of having an update when form.A is changed, if the value of form.A is going to be ignored?
Is it perhaps form.b you wish to trigger update() on?
  1 件のコメント
jeff wu
jeff wu 2012 年 3 月 14 日
yes sorry, when form.h and form.b are changed the update shall be lunshed again

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProperties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by