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

99 ビュー (過去 30 日間)
Guanyu Wang
Guanyu Wang 2017 年 10 月 13 日
コメント済み: OCDER 2017 年 10 月 16 日
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!

採用された回答

OCDER
OCDER 2017 年 10 月 13 日
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 件のコメント
Guanyu Wang
Guanyu Wang 2017 年 10 月 16 日
Thank you a lot Donald! Wish I can vote you twice haha.
OCDER
OCDER 2017 年 10 月 16 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by