0

I have 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.

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 the 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 go mouse-up the 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?

PS: I found these posts relevant but not exactly applicable to my case given the presence of nested functions.

Post 1

Post 2

Post 3

Thanks,

Alborz

Here is the code:

function varargout = plot_test(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @plot_vertline_handles_OpeningFcn, ...
    'gui_OutputFcn',  @plot_vertline_handles_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
end
function plot_vertline_handles_OpeningFcn(hObject,~,handles,varargin)
handles.output = hObject;
guidata(hObject, handles);
end
function varargout = plot_vertline_handles_OutputFcn(~,~,handles)
varargout{1} = handles.output;
end

%% Vertline Callback
function pushbutton_vertline_Callback(hObject,~,handles) %#ok<*DEFNU>
axis_h = handles.axis_h;
vertline_h = line('parent',axis_h,'xdata',[1 1],'ydata',[-1 1],...
    'ButtonDownFcn', @mouseDown );
handles.vertline_h = vertline_h;
guidata(hObject,handles)

    function mouseDown(~,~)
        mousedown = true;
        handles.mousedown = mousedown;
        guidata(hObject,handles)
    end
end

%% Plot Callback
function pushbutton_plot_Callback(hObject,~,handles)
clc;
open_figs_h = get(0,'children');
close(open_figs_h(2:end));

x = -pi:0.01:pi;
y = sin(x);

fig_h = figure('units','normalized','outerposition',[0.2 0.2 .5 .5],...
    'WindowButtonMotionFcn', @MouseMove, 'WindowButtonUpFcn', @MouseUp );
axis_h = axes('parent',fig_h,'position',[0.1 0.1 .8 .8]);
line('parent',axis_h,'xdata',x,'ydata',y);

handles.axis_h = axis_h;
guidata(hObject,handles);

    function MouseUp(~,~)
        handles = guidata(hObject);
        handles.mousedown = 0;
        guidata(hObject,handles);
    end
    function MouseMove(~,~)
        try handles = guidata(hObject);catch, end

        if isfield(handles,'mousedown')
            mousedown = handles.mousedown;
        else
            mousedown = 0;
        end

        if mousedown
            x_current = get(axis_h,'CurrentPoint' );
            vertline_h = handles.vertline_h;

            set (vertline_h, 'XData',[x_current(1,1) x_current(1,1)] );

            if isfield(handles,'x_history')
                disp('Field "x_history" Exists in handles')
                handles.x_history = [handles.x_history x_current(1,1)]
            else
                disp('Field "x_history" Does Not Exist in handles')
                handles.x_history = x_current(1,1)
            end
            guidata(hObject,handles);
        end

    end
end
Alborz
  • 19
  • 6

1 Answers1

0

With Jan Simon's guideline, I figured what was missing in the code:

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);
Alborz
  • 19
  • 6