What is the purpose of arguments 'src' and 'evnt' in callback functions?
94 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
0 commentaires
Réponse acceptée
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.
This link could help explain callback functions in general: https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-using-the-guide-workflow.html
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Object Programming 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!