MATLAB Answers

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

19 views (last 30 days)
Albert Bing
Albert Bing on 12 Oct 2019
Commented: Albert Bing on 12 Oct 2019
  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).
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!

Sign in to comment.

Answers (1)

Guillaume on 12 Oct 2019
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
then varargin is simply {}, an empty cell array.
  1 Comment
Albert Bing
Albert Bing on 12 Oct 2019
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) .

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!