How to get Dependent Property depending on properties of objects of different class

4 ビュー (過去 30 日間)
J. Hu
J. Hu 2013 年 10 月 2 日
コメント済み: Matt J 2013 年 10 月 2 日
See the code below:
Ex_ObjA.m-->
classdef Ex_ObjA
properties
a
end
methods
function Obj=Ex_ObjA(t)
Obj.a = t;
end
end
end
Ex_ObjBC.m-->
classdef Ex_ObjBC
properties
b
end
properties (Dependent = true, SetAccess = public)
c
end
methods
function Obj=Ex_ObjBC(t)
Obj.b = t;
end
function c=get.c(Obj,s1) % error: Get methods must have exactly one input
c = Obj.b + s1.a;
end
end
end
I tried to do following:
s1 = Ex_ObjA(2);
s2 = Ex_ObjBC(3);
s2.c
Not successful, because "Get methods must have exactly one input". So I can pass the s1.a to Ex_ObjBC to get s1.c?
Much appreciation!!!

採用された回答

Matt J
Matt J 2013 年 10 月 2 日
編集済み: Matt J 2013 年 10 月 2 日
There's no apparent reason why c should be a property, Dependent or otherwise. You could just write the class this way,
classdef Ex_ObjBC
properties
b
end
methods
function Obj=Ex_ObjBC(t)
Obj.b = t;
end
function out=c(Obj,s1)
out = Obj.b + s1.a;
end
end
end
  4 件のコメント
J. Hu
J. Hu 2013 年 10 月 2 日
Thanks for your explanation. Since c is a value depending on properties of different classes, what you would suggest placing the function to get c - in one of the classes of the properties it depends on or none of them? For example, if c depends on 10 properties, 9 of those belong to one class A and one comes from another class B. So c is HIGHLY depending on the class A - and that is the reason I wanted to put it is a dependent property of class A.
Upon your suggestion, I did define the function like c=getc(Obj,s1) and call it using s2.getc(s1). It works but I do not understand why the inputs have to be like that, one is (Obj,s1) in the function definition and the caller is getc(s1) - not the same. I know it works but why? Seems Obj is internal input of s2. Thanks...
Matt J
Matt J 2013 年 10 月 2 日
s2.getc(s1) is equivalent to getc(s2,s1)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by