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 CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by