Filter löschen
Filter löschen

What is the purpose of arguments 'src' and 'evnt' in callback functions?

104 Ansichten (letzte 30 Tage)
For callback functions, for example, a listener function of an event, the syntax is
function my_callback(obj,src,evnt)
...
end
However, even if the arguments 'src' and 'evnt' are not used in the body of 'my_callback' function, the unused arguments are still required to appear in the parentheses. I can replace them with '~':
function my_callback(obj,~,~)
...
end
But the unused arguments always need 2 positions when declaring the function. If I write 'my_callback' without the two '~':
function my_callback(obj,~,~)
...
end
Matlab will throw an error stating 'too many arguments'. What is the reason? Thank you!

Akzeptierte Antwort

OCDER
OCDER am 13 Okt. 2017
Matlab is essentially passing on a standard set of information for any object that has a callback feature: 1) the object that is affected, 2) the event that took place, and 3) the GUI data. Even if you don't use the 2nd and 3rd inputs, Matlab still has to pass on these information for consistency. By removing your 2nd and 3rd input, Matlab will complain as it tries to do this:
@your_callback_fcn(obj, event, data) %ERROR if your_callback_fcn accepts only 1 input.
Here's a copy of the relevant text from that link:
function pushbutton1_Callback(hObject,eventdata,handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
end
All callbacks must accept at least three input arguments:
hObject — The UI component that triggered the callback.
eventdata — A variable that contains detailed information about specific mouse or keyboard actions.
handles — A struct that contains all the objects in the UI. GUIDE uses the guidata function to store and maintain this structure.
  4 Kommentare
Guanyu Wang
Guanyu Wang am 16 Okt. 2017
Thank you a lot Donald! Wish I can vote you twice haha.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance 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!

Translated by