Matlab Class property sharing between different classes

11 ビュー (過去 30 日間)
Anil Ozturk
Anil Ozturk 2018 年 11 月 20 日
コメント済み: madhan ravi 2018 年 11 月 21 日
Hello everyone,
I have two different class implementations:
classdef X classdef Y
properties properties
a a
b c
end end
... ...
end end
I create an object from X and an object from Y. Then, I need the memory to be shared between X.a and Y.a such that if I update X.a, I want Y.a to be updated, not the other properties (b and c) be afffected. I tried different techniques such as creating handle classes for X and Y and made an equality equation such that
X.a = Y.a ;
However, the memory addresses are not shared, only the value is copied from Y class to the X class. If I change X there is no effect on Y class.
What is the method for sharing the memory of properties between different classes?

採用された回答

TADA
TADA 2018 年 11 月 20 日
If you Trying Just copy the Property Value To Share The Value, That Can't Be Done Unless That Value Is A Third Handle Class
classdef ByRefProp < handle
properties
value;
end
end
Now you Can Set
x = X(); y = Y();
y.a = ByRefValue();
x.a = y.a;
y.a.value = 'The Quick Brown Fox Jumps Over The Lazy Dog';
disp(x.a.value);
More importantly, You Should Consider The Relationship Between X and Y.
You May Need One To Contain Or Inherit The Other.
  2 件のコメント
Anil Ozturk
Anil Ozturk 2018 年 11 月 21 日
Thank you for the quick response, it works!
But i preffered using the matlab.mixin.Copyable class mechanism for the object memory share.
I used the example given in matlab reference in :
madhan ravi
madhan ravi 2018 年 11 月 21 日
+1

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClass Introspection and Metadata についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by