MATLAB Answers

0

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

Asked by Chuan Kui Sun on 12 Oct 2019
Latest activity Commented on by Chuan Kui Sun 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).
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 Comments

"I was told to use function varargout = someUI_Callback(varargin) to write the Callback functions."
What strange and pointless advice.
Callback functions are not called with any output arguments, so the varargout serves absolutely no point whatsoever. And using varargin with a small, fixed number of input arguments is pointlessly complex: it is simpler and more efficient to just write the two/three input arguments explicitly (which in turn makes the code self-documenting and provides useful code-completion tips).
So you should actually be writing your callbacks just like the MATLAB documentation shows, e.g.:
Very commonly the first input is named obj and the second evt:
function mycallback(obj,evt)
"How do you access handles in the programmatical GUI Callback..."
The standard way is to use guidata.
But if you are writing your own code then I would recommend that you ignore everything that you read about GUIDE (which is pointlessly complex and indirect) and simply use nested functions rather than a handles structure. Just because GUIDE uses a handles structure does not mean that you have to do the same yourself:
Callback functions are just functions, and there is no reason why you have to follow GUIDE's rather indirect, obfuscated, and complex ways of doing things.
Apart from the valuable advice from Stephen and Guillaume, you might benefit from this thread.
Thanks! Very instructive!
The callback definition helps a lot.

Sign in to comment.

1 Answer

Answer by 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
myfunc()
then varargin is simply {}, an empty cell array.

  1 Comment

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) .

Sign in to comment.