Effacer les filtres
Effacer les filtres

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

104 vues (au cours des 30 derniers jours)
Guanyu Wang
Guanyu Wang le 13 Oct 2017
Commenté : OCDER le 16 Oct 2017
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!

Réponse acceptée

OCDER
OCDER le 13 Oct 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 commentaires
Guanyu Wang
Guanyu Wang le 16 Oct 2017
Thank you a lot Donald! Wish I can vote you twice haha.
OCDER
OCDER le 16 Oct 2017
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by