Matlab GUI - 鼠标点击回调

时间:2015-04-27 20:01:32

标签: matlab matlab-guide

我正在使用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,我认为即使在轴上有一个绘图,点击也应该有效,但它并没有。有什么建议吗?

谢谢!

克劳斯

Update1:​​@matlabgui 我试图将NextPlot更改为在您的示例中添加,但它仍然无法正常工作。我认为在这一点上,我对MATLAB / GUI不够熟悉,无法正确理解你的建议。

我希望这不是太多要求,但如果我刚为Axes本身创建了一个ButtonDownFcn(空)并绘制了如下代码中的图形。我需要添加什么来显示代码"单击轴"在我点击显示图形的轴后,在我的命令窗口中(无论是否点击图中的空白区域或线条本身)?我认为最简单的例子是在我的代码中有效,然后逐步分析。

绘图编码:

axes(handles.Axes_1); 
plot(x,y);

清空bdfcn:

function Axes_1_ButtonDownFcn(hObject, eventdata, handles)

1 个答案:

答案 0 :(得分:1)

以下行无效,因为情节句柄HitTest已关闭h

set(h,'ButtonDownFcn','disp(''axis callback'')')

您需要保持轴(或将NextPlot属性更改为'add' - 否则在创建新图时 - 轴的ButtonDownFcn回调将被清除。< / p>

见下面的例子:

%% 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 )} );