This is an extension to a 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? 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. Mar 20, 2014 - CalledComponent_Callback(handles.CalledComponent, [], handles). Inside this function I am updating the handles as: guidata(hObject, handles). But once I get out the variables included to handles in the CalledComponent are no longer available. I have been reading some answers to similar questions,. GUIData not updating properly. You can utilize guidata in update_display to get and store your handles. (hObject,eventdata,hfigure) handles = guidata(hfigure. 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? Jan, Appreciate your time. Here is a more detail description of the problem: As I click on the line and drag it, the 'x_history' variable records the x coordinate of vertical line. The length of 'x_history' increase by adding new x coordinates to it. This variable is being saved into handles via guidata. Now I stop moving, release the mouse (mouse-up) and then click on the line and start to drag it again. Problem is that at this stage I would like to retrieve the previous x_history vector and continue adding x coordinates to it. But what I get when I start to drag the line is handles.x_hisory=[]. That's what I meant by 'handles resets x_history field'. Given your excellent hint, I think I figured what I've been missing: In the mouseDown callback (the nested function in pushbutton_vertline_Callback), I was missing to get the updated copy of handles. So, I needed to add this line to that nested function: handles = guidata(hObject); This resolves the issue so every time I resume dragging the line I can retrieve the previous record of x_history. Now my question is answered (thanks:)), but I'm still a bit confused about your comment on complexity of the code. I am not using handle of the nested function anywhere within the code. As you can see all input arguments of the nested functions are replaced by ~. So, anytime I grab handles it's the one that is being retrieved from guidata. So, do you still recommend to change the nested functions to ' non-nested normal' functions? Alborz to comment.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
July 2018
Categories |