Input Arguments for Callback Function in a listener
조회 수: 3 (최근 30일)
이전 댓글 표시
I am trying to use a listener to update one GUI when the data in another GUI changes. I think I got the listener defined, at least I can see that it has been added to the handle structure, but I dont understand what I should set the inputs to the callback function to. From the documentation I can see that that inputs must be a object handle and event.EventData object but I'm not quite sure what that refers to.
%Get GUI_1 data
gui1_handle = findobj('tag','GUI1');
gui1_data = guidata(gui1_handle);
% Add listener to GUI_2 with trigger from GUI_1
handles.listener=addlistener(gui1_data.pitch1,'Value','PostSet',@updategui2);
% Update handles structure
guidata(hObject, handles);
function updategui2(?????,??????)
gui1_handle = findobj('tag','GUI1');
gui1_data = guidata(gui1_handle);
x=num2str(guidata(gui1_data.pitch1))
set(handles.text3,'string',x);
Thank you for your help
댓글 수: 2
채택된 답변
Stephen23
2017년 2월 15일
편집: Stephen23
2017년 2월 15일
If you do not need to use those inputs, then define your function using input placeholders:
function updategui2(~,~)
FYI, the object handle is quite useful if you want to know the value or status of the calling object, e.g. what value a button or text box has.
추가 답변 (1개)
Guillaume
2017년 2월 15일
편집: Guillaume
2017년 2월 15일
Yes, I think matlab's doc does a poor job of explaining the arguments that get passed to a callback. Thankfully, they use the same convention used by many other languages for events. An event callback always receives two arguments:
- hObject, the object that triggered the event. If the callback is associated with a single object, then you don't really need to capture this argument (use ~), e.g. in your code, it's always going to be gui_data.pitch1. However if you associate the callback with events from different objects, you can use this first argument to find out which of these objects triggered the event.
- eventData, an object of class event.EventData or a derived class that contains properties specific to the event (e.g. for a mouse event it could contain the mouse location, or the button states). In matlab, more often than not this is empty (so can be safely ignored) and when it's not, what is in there is often not documented.
As for your problem, make sure that the property you're listening for change ( String or Value ?) has the SetObservable attribute.
댓글 수: 2
Guillaume
2017년 2월 15일
I assume that pitch1 is a class that is defined by your code. Its code must look something like:
classdef Pitch1 < handle
properties
String;
Value;
SomeOtherProp;
%...
end
%...
end
Move the properties you want to see changes for into their own properties block with a SetObservable attribute:
classdef Pitch1 < handle
properties
SomeOtherProp;
%...
end
properties (SetObservable)
String;
Value;
%...
end
%...
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!