MATLAB: Does the execution of the callback function cause another figure to be opened instead of acting on the GUI in MATLAB

figuregcagcbfgcfgcoMATLABnew

I have written my own callback functions that should perform some action in my GUI. However, when the callback executes, it causes another figure to open.

Best Answer

This can occur if you are using GCF in your callback function. GCF is a convenience function that returns the handle of the root object's "CurrentFigure" property. This property can only hold a figure whose handle is visible, i.e. the HandleVisibility is "on" (or "callback" while a callback is executing). If the figure's "HandleVisibility" is "off", then its handle will not be stored in the root object's "CurrentFigure" property. In this case, GCF will return either another figure (whose "HandleVisibility" is "on") or a new figure (if there are no figures with the appropriate "HandleVisibility" value).
Instead of GCF, you should use either GCBF, or the handle of the figure from the "handles" structure. GCBF will return the handle of the figure that contains the current callback object, i.e. the last object interacted with. The "handles" structure should contain the handle for the figure automatically, unless its Tag was modified and can not be used as a fieldname.
For instructions on plotting into axes in your GUI, see the Related Solution listed below: