MATLAB: How to save (or prevent from being deleted) handle to axes within a GUI (created using GUIDE)

axesguiguidehandlesplot

My GUI contains a single axes space. Handle to that axes on GUI initialization is handles.axes1. I have enabled multiple callback buttons on the GUI to plot 1) scatter, 2) line plot, 3) bar graph. I set axes to handles.axes1 using axes(handles.axes1). Upon initialization, isvalid(handles.axes1) = 1, and all plots turn out within this axes space, however, after one round of plotting 1 – 3 plots above, when I re-click on any, Matlab returns error message displaying, handles to axes not found, i.e., handles.axes1 points to a deleted object. I am not deleting handles.axes1 during any of the callbacks. How to prevent this deletion? How to effectively save/re-initialize handles.axes1 without having to reinitialize GUI? Even if I save axes handles into another object, the object gets deleted eventually. Because of this I have to manually set axes position and on multi-platform this is not resulting in favorable results, the plots get partially hidden (or shifted to right/left/top/bottom of axes space) depending on screen resolution, version of matlab etc. Favorable outcome will be, I get to save (or prevent from deletion) handles.axes1 and keep using it to set axes between multiple callbacks for plotting.

Best Answer

Search your code for "clear" - chances are you're calling clear somewhere in a callback and this blows away the handle structure inside any function where you called clear.