我在指南中有一个非常简单的GUI,我有一个由按钮启动的绘图功能,它在轴上绘制一个散点图(称为Method1axes1):
handles.plot = scatter(X,Y, 'parent', handles.Method1axes1);
现在我希望用户能够单击轴(绘图)以获得更大的数字。我试过下面的代码,如果我不首先在轴上绘图,这是有效的。一旦我运行绘图功能,散点图就会显示在Method1axes1中,但我无法再单击该图。
% --- Executes on mouse press over axes background.
function Method1axes1_ButtonDownFcn(hObject, eventdata, handles)
figure
scatter(X,Y);
我究竟做错了什么?
这是MATLAB的一种特殊情况,并没有很好的记录。
您需要考虑两件事:
axes
中绘制一些东西时,情节就在前景上。所以当你点击你的axes
时,顶部的情节拦截了那个点击并试图处理它。您需要从axes
中的绘图/散点图像对象中禁用鼠标单击捕获。为此,您必须将散点图对象的HitTest
属性设置为'off'
。 (最近的MATLAB版本更改了此属性的名称,现在称为PickableParts
)。axes
ButtonDownFcn
回调的文档中,但它不再被解释(虽然行为持续存在)。这是我在旧论坛上可以找到的:当您调用PLOT时,如果轴
NextPlot
属性设置为'replace'
(默认情况下),则axes
(包括ButtonDownFcn
)的大部分属性都将重置为其默认值。将
axes
NextPlot
属性更改为'replacechildren'
以避免这种情况,或在调用PLOT后设置ButtonDownFcn
,或使用低级LINE函数而不是更高级别的PLOT函数。
这也在这里讨论和解释:Why does the ButtonDownFcn callback of my axes object stop working after plotting something?
对于你的情况,我尝试了set(axe_handle,'NextPlot','replacechildren')
,它可以让鼠标点击到达ButtonDownFcn
,但遗憾的是它会造成对axes
限制和LimitModes的破坏......所以我选择了第二个解决方案,即重新定义ButtonDownFcn
的回调在axes
的每个阴谋之后。
总而言之,你的pushbutton1_Callback
代码应该是:
function pushbutton1_Callback(hObject, eventdata, handles)
% Whatever stuff you do before plotting
% ...
% Plot your data
handles.plot = scatter(X,Y, 'parent', handles.Method1axes1);
% Disable mouse click events for the "scatterplot" object
set(handles.plot,'HitTest','off') ;
% re-set the "ButtonDownFcn" callback
set(handles.Method1axes1,'ButtonDownFcn',@(s,e) Method1axes1_ButtonDownFcn(s,e,handles) )
对于你的axes
鼠标点击事件,你也可以保留新生成的对象的句柄:
function Method1axes1_ButtonDownFcn(hObject, eventdata, handles)
handles.newfig = figure ;
handles.axes1copy = copyobj( handles.Method1axes1 , handles.newfig ) ;
请注意,我只需使用copyobj
函数,而不是绘制新的集合,在需要重现绘图时非常方便。
插图: