0

我有一个带有 2 个按钮的简单指南:

按钮 #1 绘制正弦图。

按钮 #2 将一条“可移动”垂直线添加到图形中。

一切正常,我可以根据需要拖动垂直线。

我现在要做的是在我移动垂直线(x_history)时记录它的x位置并将这个x_history保存到句柄(通过guidata),这样如果我停止移动线(鼠标向上)然后继续移动线(鼠标向下和鼠标移动) 我可以从句柄恢复以前的 x_history(通过 guidata)。

我面临的问题是每次鼠标向上时,手柄都会重置(!) x_history 字段(删除该字段),所以当我恢复鼠标向下并移动线时,我丢失了 x_history 的先前记录。

我在这里想念什么?

PS:考虑到嵌套函数的存在,我发现这些帖子相关但并不完全适用于我的案例。

帖子 1

帖子 2

帖子 3

谢谢,

厄尔布尔士

这是代码:

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
4

1 回答 1

0

根据 Jan Simon 的指导方针,我发现了代码中缺少的内容:

mouseDown回调( pushbutton_vertline_Callback中的嵌套函数)中,我缺少获取句柄的更新副本。因此,我需要将此行添加到该嵌套函数中:

handles = guidata(hObject);
于 2017-08-06T19:07:12.477 回答