MATLAB: GUIDATA updating “handles” in GUIDE with nested functions

guiguidehandlesvertical line

This is an extension to a current question answered by Geoff Hayes and Walter Roberson:
Attached you can find a simple GUIDE with 2 push buttons:
Button #1 plots a sine graph.
Button #2 adds a "movable" vertical line to the graph.
Everything works fine and I can drag vertical line as desired (Thanks to Geoff and Walter)
What I'm trying to do now is to record x-position of vertical line (x_history) as I move it, and save this x_history to handles (via guidata) such that if I stop moving the line (mouse-up) and then resume moving the line (mouse-down and mouse-move) I can restore previous x_history from handles (via guidata).
The problem I'm facing is that every time I stop moving the line and go mouse-up, handles resets(!) the x_history field (removes the field), so I loose the previous record of x_history when I resume mouse-down and move the line.
What am I missing here?
Alborz

Best Answer

This is a confusing mixture of storing the handles struct in two figures by guidata, using handles from the inputs of the callbacks (where it is a static copy of the contents from this struct at the time of the creation of the callback), and the common using of local variables in nested functions. This is more complexity than you require.
Decide for one mechanism to share the handles struct, e.g. in the figure's ApplicationData by using guidata and prefer the figure, which contains the activated object. Then insert this in each callback:
function XYZ_Callback(hObject, EventData, handlesFromInput)
handles = guidata(hObject);
...
guidata(hObject, handles);
end
Convert the nested functions to "non-nested normal" (how is it called?) function and use the input hObject.
In addition the guidata concern the figure, which contain the figure with the two buttons, not the figure, which contains the diagram and the moved line. Better keep your data locally in the figure, which is processed.
By the way: I could not reproduce your observation:
The problem I'm facing is that every time I stop moving the line and go
mouse-up, handles resets
How and where do you observe this?