Input Arguments for Callback Function in a listener
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stig Eriksen
am 15 Feb. 2017
Kommentiert: Stig Eriksen
am 15 Feb. 2017
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 Kommentare
Akzeptierte Antwort
Stephen23
am 15 Feb. 2017
Bearbeitet: Stephen23
am 15 Feb. 2017
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 Kommentar
Weitere Antworten (1)
Guillaume
am 15 Feb. 2017
Bearbeitet: Guillaume
am 15 Feb. 2017
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 Kommentare
Guillaume
am 15 Feb. 2017
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
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!