MATLAB: Does the output from GUIHANDLES change after I plot to the GUI in MATLAB

buttondownfcncallbackcallbacksclearclearedguiguideguihandlesMATLABpropertiesreset

I created a GUI that contains axes that I plot into. The plotting is performed in the callback for a UICONTROL in the GUI. However, after plotting once, the "handles" structure that is returned by GUIHANDLES has changed, and the axes' handle is no longer listed.

Best Answer

This behavior is the result of a combination of the side effects of PLOT, the axes' "NextPlot" property, and the use of GUIHANDLES.
PLOT makes a call to the NEWPLOT function to determine which axes to use. Also, depending on the axes' "NextPlot" property, some of that axes' properties may be set to default values. If the property has the value "Replace", it will reset all of the axes' properties and delete all of the children of the axes. This includes the axes' "Tag" property, set to an empty string. If the "NextPlot" property is set to "ReplaceChildren", then NEWPLOT will only delete the axes' children and will not change the axes' other properties.
GUIHANDLES creates a structure containing the handles for all figure objects having a non-empty "Tag" value (thus having a valid fieldname for a structure). Since the axes' Tag will be an empty string after plotting, it will not be included in the handles structure that is output by GUIHANDLES.
There are two ways around this problem:
1) Do not use GUIHANDLES to get the "handles" structure. If you are using GUIDE and are working in a GUIDE-generated callback function, the "handles" structure will already exist as an input to the function. Also, GUIHANDLES is only intended for use in initializing the GUI. After the GUI is initialized, use the GUIDATA function to get the "handles" structure since the structure may be modified.
2) Set the axes' NextPlot property to 'replacechildren'. This way, the axes' Tag property won't be changed when PLOT calls NEWPLOT.