Input Arguments for Callback Function in a listener

조회 수: 3 (최근 30일)
Stig Eriksen
Stig Eriksen 2017년 2월 15일
댓글: Stig Eriksen 2017년 2월 15일
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
Adam
Adam 2017년 2월 15일
Where exactly is pitch1 stored and what is it?
Stig Eriksen
Stig Eriksen 2017년 2월 15일
Dammit, you are right, pitch1 is a variable not a handle??? Well that didnt take me long :-) Thanks for all the help

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
Stig Eriksen
Stig Eriksen 2017년 2월 15일
Thank you for your answer Stephen.
I tried that and still cannot make it work. It is possible that my mistake in not only related to the input argument. The updategui2 function works (I forced it to run with a button push) but does not seem to be triggered by the listener. I can also see that the listener gets added to the handle structure if I call it but am not at all sure if the properties are correct:
proplistener with properties:
Object: {[1x1 UIControl]}
Source: {[1x1 matlab.graphics.internal.GraphicsMetaProperty]}
EventName: 'PostSet'
Callback: @updategui2
Enabled: 1
Recursive: 0
I tried the (~,~), (gui1_data,~), (gui1_data.pitch1,~) and (pitch1,~) with no success.
%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,'String','PostSet', @updategui2);
% Update handles structure
guidata(hObject, handles);
function updategui2(pitch1,~)
gui1_handle = findobj('tag','GUI1');
gui1_data = guidata(gui1_handle);
x=get(gui1_data.pitch1,'String');
set(handles.text3,'String',x);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
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:
  1. 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.
  2. 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
Stig Eriksen
Stig Eriksen 2017년 2월 15일
I think I am a little out of my depth here. To change the attribute I need to alter the class definition right? I have not defined my own classes, how do I access and alter the SetObservable attribute?
Guillaume
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 CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by