Input Arguments for Callback Function in a listener
Mostrar comentarios más antiguos
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 comentarios
Adam
el 15 de Feb. de 2017
Where exactly is pitch1 stored and what is it?
Stig Eriksen
el 15 de Feb. de 2017
Respuesta aceptada
Más respuestas (1)
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 comentarios
Stig Eriksen
el 15 de Feb. de 2017
Guillaume
el 15 de Feb. de 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
Categorías
Más información sobre Entering Commands en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!