フィルターのクリア

Linking fields of a structure

2 ビュー (過去 30 日間)
Mahitha
Mahitha 2014 年 8 月 19 日
コメント済み: Mahitha 2014 年 8 月 19 日
Hello,
I have a scenario where two fields of a structure have a relationship. My question is, how to change one field automatically, if the other is changed manually?
Say, The structure is Amp with fields a,b,c,d. Now, Amp.a=Amp.b+10. My question is, how to update Amp.a, when I change Amp.b?
I know that it is a redundant field and I can use "Amp.b+10" wherever required. But I have multiple blocks with such a need and the readabilty of the equations is affected.
Note that I am using these variables in Simulink blocks and not directly in Matlab code. So, I should be able to save the workspace as per my requirement prior to simulations.
Any help is much appreciated!

採用された回答

Matt J
Matt J 2014 年 8 月 19 日
You would need to write your own class and use that instead of the structure. But once you do, it's the kind of thing Dependent class properties do very readily.
  2 件のコメント
Matt J
Matt J 2014 年 8 月 19 日
編集済み: Matt J 2014 年 8 月 19 日
As an illustration, the class down on that bottom lets you define an object for which S.b=S.a+10 always. Here are examples of its usage:
>> S=myclass; S.a=1:3; S.b
ans =
11 12 13
>> S.a(2)=5; S.b(2)
ans =
15
classdef myclass
properties
a
end
properties (Dependent=true,SetAccess=private)
b
end
methods
function val=get.b(obj)
val=obj.a+10;
end
end
end
Mahitha
Mahitha 2014 年 8 月 19 日
Thank you.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by