classes and the "set" method

2 ビュー (過去 30 日間)
kk
kk 2020 年 5 月 26 日
コメント済み: kk 2020 年 5 月 27 日
Hi,
I am trying to write a class in Matlab and I am new to it, sorry that I probably don't use the correct terminology. In a basic example, I would like to have data structured in this way:
classdef DummyBasic
properties
av = 1;
end
methods
end
end
and
classdef Dummy
properties
X = 0;
a = DummyBasic;
end
methods
end
end
Now, I would like to change X whenever a.av is changed. I guess it would be easy to do if a rearranged the properties X and av to a single class. But is it possible to somehow keep this structure (I am using Matlab2015b)?
Any help appreciated.

採用された回答

Steven Lord
Steven Lord 2020 年 5 月 26 日
Make X a Dependent property in class Dummy and make its get.X method compute the value of X using the current value of the av property. In the example on that documentation page X would behave like the Balance property while av would fill the role of the Currency and DollarAmount properties.
While I don't think this documentation page has changed significantly since release R2015b, if you want to be sure search your locally-installed documentation for "dependent property" and refer to the information on the release R2015b version of this page.
  1 件のコメント
kk
kk 2020 年 5 月 27 日
Thank you, this is very helpful, I can now see how the structuring in Matlab works. Because I also want to store data in X, I decided it would be easier for me to define a function setAv() in the Dummy class, which will set the a.av and recalculate the X, instead of directly changing the a.av.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by