Circular Listeners (2 objects listening to each other)

2 ビュー (過去 30 日間)
Greg
Greg 2018 年 1 月 12 日
回答済み: Greg 2018 年 1 月 17 日
I have a data class and a GUIDE figure to help interact with the data. One of the properties of the class is a threshold value, so the figure has a threshold textbox. I'd like to set up listeners such that changing either side (the textbox "string" or the data class property) will update the other.
I was afraid I'd end up in an infinite loop, but it appears MATLAB is smart enough to only execute each listener once. However, this means that 2 events fire for a single value change. This is further complicated by the fact that the textbox uses the "string" property, so the data class ends up being character, not numeric.
Datavar.Value = 1;
% First listener fires, sets edit1.String to '1'
% Second listener fires, sets Datavar.Value to '1'
Datavar.Value
ans =
'1'
I've attached a minimal working example. To test it:
a = gui1;
handles = guidata(a);
EL = handles.TestEL_Object;
EL.Property1 = 8;
EL.Property1
I could utilize the "UserData" property and implement a str2double call, so at least the double-firing wouldn't change the value. Any ideas on getting it to only fire one event? I can imagine my situation getting more computationally expensive, so the second execution would hurt.
  1 件のコメント
Greg
Greg 2018 年 1 月 12 日
Also, I'd love to convert my GUIDE to AppDesigner, but Set/Get -Access Observability is 'off' for almost everything in AppDesigner.

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

採用された回答

Greg
Greg 2018 年 1 月 17 日
Interesting, .mlapp files cannot be uploaded to MATLAB Answers...
I have solved the problem myself, but I'm still open to better (cleaner, easier, etc.) solutions.
For the class variable use a dependent public property whose:
  • Set method transfers the value to a private property
  • Get method retrieves the value from that private property
Have the class's listener set the private property directly. This decouples the listener from the event (the other objects still listen to the public property set event). Also, set the GUIDE listener to the 'Action' event and the AppDesigner listener to a manually-created 'NumChanged' event.
For the GUIDE code:
  • Listen to the public property's 'PostSet' event
  • The rest is already decoupled because handles.edit1.String = 5; does not trigger the 'Action' event - only user interaction with the actual edit control does
For the AppDesigner code:
  • Manually define a 'NumChanged' event
  • Have the EditField's ValueChanged callback explicitly notify the 'NumChanged' event
  • Again, decoupled because programmatic 'Value' setting does not trigger the callback
Download the attached files for the gory details. I haven't attempted to get all 3 to listen to each other, but that's next on my list for playtime.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by