我試圖在軸上繪製一條線,點擊時只要按下鼠標按鈕,當按鈕釋放時,線將停止跟隨。簡而言之,根據點擊和拖動將圖形上的一條線重新定位到新的位置。WindowButtonUpFcn問題:它爲什麼不釋放?
我已經能夠開始讓線跟隨鼠標指針,問題是得到WindowButtonUpFcn停止跟隨鼠標線。即如何關閉WindowButtonMotionFcn?
這是代碼。這很粗糙,因爲它只是一個小型測試程序,所以不要批評太多。
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = 0:.1:10;
y = zeros(size(x))+.5;
line(x,y, 'Tag', 'newLine');
set(findobj('Tag', 'newLine'),'ButtonDownFcn',@button_down)
end
function button_down(src,evnt)
% src - the object that is the source of the event
% evnt - empty for this property
set(src,'Selected','on')
set(gcf, 'WindowButtonMotionFcn', @button_motion);
end
function button_motion(src, evnt)
h = findobj('Tag', 'axes1');
pos = get(h, 'CurrentPoint');
disp(pos);
hndl = findobj('Tag', 'newLine');
delete(hndl);
x = 0:.1:10;
y = zeros(size(x))+pos(3);
line(x,y, 'Tag', 'newLine');
set(gcf, 'WindowButtonUpFcn', @button_up);
end
function button_up(src, evnt)
%What to do here?
end
制定出很好,謝謝你gnovice。 – Michael 2010-07-04 03:23:47
+1爲完整的解決方案,並彌補缺乏接受。 – Jonas 2010-07-04 19:05:20