Matlab GUI - 鼠标点击的轴回调
Matlab GUI - Axes callback for mouseclick
我正在用 matlab 编写 GUI,不知何故我在轴上鼠标单击的回调函数方面遇到了问题。我找到了一些类似的主题,但那里给出的解决方案无法解决我的问题。
我的代码的重要部分如下所示(首先使用 Axes 的 ButtonDownFcn 进行正常尝试,只要我不绘制任何东西,它就可以工作):
function Axes_1_ButtonDownFcn(hObject, eventdata, handles)
disp('axis callback');
(我想关闭 HitTest 的第二次尝试,这个根本不起作用)
axes(handles.Axes_1);
h=plot(x,y);
set(h,'HitTest','off');
set(gcf,'WindowButtonDownFcn','disp(''axis callback'')')
set(h,'ButtonDownFcn','disp(''axis callback'')')
自从我关闭 HitTest 后,我认为即使在坐标轴上有绘图,点击也应该有效,但事实并非如此。有什么建议吗?
谢谢!
克劳斯
更新1:@matlabgui
我尝试更改 NextPlot 以添加您的示例中的内容,但它仍然不起作用。我认为目前我对 MATLAB/GUI 还不够熟悉,无法正确理解您的建议。
我希望这不是要求太多,但如果我只是为 Axes 本身创建一个 ButtonDownFcn(空)并绘制如下代码所示的图形。单击显示图形的轴后,我必须在代码中添加什么才能在命令 window 中显示 "Single click on axis"(无论我是否单击空的 space在情节或线条本身)?我认为最简单的方法是在我的代码中使用一个简短的示例,然后逐步分析它。
绘图代码:
axes(handles.Axes_1);
plot(x,y);
空 bdfcn:
function Axes_1_ButtonDownFcn(hObject, eventdata, handles)
以下行无法工作,因为 HitTest
对于绘图句柄 h
已关闭
set(h,'ButtonDownFcn','disp(''axis callback'')')
您需要保持坐标轴(或将 NextPlot
属性 更改为 'add'
- 否则当您创建新绘图时 - 您的 ButtonDownFcn
回调轴将被清除。
请参阅以下示例:
%% This is what you have to start with
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) )
%% This doesn't work -> as the plot command is clearing the axes which also clears the ButtonDownFcn
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); plot ( [1:10], [1:10] );
%% The Callback is retained by changing the axes NextPlot property
f = figure; axes ( 'parent', f, 'NextPlot', 'add', 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); plot ( [1:10], [1:10] );
%% This also works by using hold on.
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); hold on; plot ( [1:10], [1:10] );
更新 1
将您的代码更改为以下(未经测试的代码):
set ( handles.Axes_1, 'NextPlot', 'add' );
plot(handles.Axes_1, x,y);
% If you don't have the buttondownfcn set you add it by:
set ( handles.Axes_1, 'ButtonDownFcn', {@Axes_1_ButtonDownFcn ( handles )} );
我正在用 matlab 编写 GUI,不知何故我在轴上鼠标单击的回调函数方面遇到了问题。我找到了一些类似的主题,但那里给出的解决方案无法解决我的问题。
我的代码的重要部分如下所示(首先使用 Axes 的 ButtonDownFcn 进行正常尝试,只要我不绘制任何东西,它就可以工作):
function Axes_1_ButtonDownFcn(hObject, eventdata, handles)
disp('axis callback');
(我想关闭 HitTest 的第二次尝试,这个根本不起作用)
axes(handles.Axes_1);
h=plot(x,y);
set(h,'HitTest','off');
set(gcf,'WindowButtonDownFcn','disp(''axis callback'')')
set(h,'ButtonDownFcn','disp(''axis callback'')')
自从我关闭 HitTest 后,我认为即使在坐标轴上有绘图,点击也应该有效,但事实并非如此。有什么建议吗?
谢谢!
克劳斯
更新1:@matlabgui 我尝试更改 NextPlot 以添加您的示例中的内容,但它仍然不起作用。我认为目前我对 MATLAB/GUI 还不够熟悉,无法正确理解您的建议。
我希望这不是要求太多,但如果我只是为 Axes 本身创建一个 ButtonDownFcn(空)并绘制如下代码所示的图形。单击显示图形的轴后,我必须在代码中添加什么才能在命令 window 中显示 "Single click on axis"(无论我是否单击空的 space在情节或线条本身)?我认为最简单的方法是在我的代码中使用一个简短的示例,然后逐步分析它。
绘图代码:
axes(handles.Axes_1);
plot(x,y);
空 bdfcn:
function Axes_1_ButtonDownFcn(hObject, eventdata, handles)
以下行无法工作,因为 HitTest
对于绘图句柄 h
set(h,'ButtonDownFcn','disp(''axis callback'')')
您需要保持坐标轴(或将 NextPlot
属性 更改为 'add'
- 否则当您创建新绘图时 - 您的 ButtonDownFcn
回调轴将被清除。
请参阅以下示例:
%% This is what you have to start with
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) )
%% This doesn't work -> as the plot command is clearing the axes which also clears the ButtonDownFcn
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); plot ( [1:10], [1:10] );
%% The Callback is retained by changing the axes NextPlot property
f = figure; axes ( 'parent', f, 'NextPlot', 'add', 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); plot ( [1:10], [1:10] );
%% This also works by using hold on.
f = figure; axes ( 'parent', f, 'ButtonDownFcn', @(a,b)disp ( 'button down on axes' ) ); hold on; plot ( [1:10], [1:10] );
更新 1
将您的代码更改为以下(未经测试的代码):
set ( handles.Axes_1, 'NextPlot', 'add' );
plot(handles.Axes_1, x,y);
% If you don't have the buttondownfcn set you add it by:
set ( handles.Axes_1, 'ButtonDownFcn', {@Axes_1_ButtonDownFcn ( handles )} );