What is the default varargin in the GUI callbacks? What are the differences between varargin{1} and varargin{2}.Source?

3 ビュー (過去 30 日間)
  1. I have been writing GUI programmatically. And I was told to use function varargout = someUI_Callback(varargin) to write the Callback functions.
  2. I checked the manual of GUIDE, and it gave examples using function someUI_Callback(hObject, eventdata, handles).
So,
1. what is the default varargin in the first method? I checked it and it looked like, that varargin{1} was the UI component itself, just like the hObject in the 2nd method. Is this right?
2. The varargin{2} had 2 field, varargin{2}.Source was 1x1 opaque. What is an opaque? Not enough material in the manual. varargin{2}.EventName was a struct, whose fields and values were the same with varargin{1}. Are varargin{1} and varargin{2}.EventName the same?
3. In the GUIDE Callbacks, all the GUI components can be accessed by handles.TagName. How do you access handles in the programmatical GUI Callback except obj = findobj('Tag', tagname)?
Thank you very much!
  3 件のコメント
Rik
Rik 2019 年 10 月 12 日
Apart from the valuable advice from Stephen and Guillaume, you might benefit from this thread.
Albert Bing
Albert Bing 2019 年 10 月 12 日
Thanks! Very instructive!
The callback definition helps a lot.

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

回答 (1 件)

Guillaume
Guillaume 2019 年 10 月 12 日
Yes, as Stephen says in his very thorough comment, that advice is utter rubbish. Don't do that it pointlessly obfuscate the code and will certainly confuse the reader as to why you've got a varargout which you will be force to set empty.
You use varargin to let the user give you optional arguments, so if you were going to use that in a callback, the signature may be something like:
function mycallback(hsource, eventargs, varargin)
You always know that you're going to receive the first two arguments for standard callbacks but it may be that you've defined a special callback that may receive extra optional arguments. This is not the default and probably a very rare thing to do. Unless you know what you're doing it's not something you should use.
As to your question, "What is the default varargin", I don't think you understand what varargin is. There is no default, varargin is simply a cell array containing all the inputs the caller passed to your function, so with a function
function myfunc(varargin)
If it's called with
myfunc(1, 2, 3)
then varargin is {1, 2, 3}. If it's called with
myfunc(hsource, eventargs) %where hsource is a GUI control and eventargs is a structure
then varargin is indeed a cell array whose first element is the control and the 2nd one is a structure. But if it's called with
myfunc()
then varargin is simply {}, an empty cell array.
  1 件のコメント
Albert Bing
Albert Bing 2019 年 10 月 12 日
Thanks!
About the varargin, I specially meaned that in the GUI callback function.
But anyway, you made it clear to me.
I'll drop the way as function someUI_Callback(varargin) .

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

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by