'ButtonDownFcn' not working on first click

1 次查看(过去 30 天)
Hi,
I'm trying to write a simple code to drag lines on a plot, and then get the X values. It worked well for some time, but I can't get it to work anymore; so, I tried the following tutorial code, that doesn't work either:
function test
f = figure;
aH = axes('Xlim',[0 1],'Ylim',[0 1]);
h = line([0.5 0.5],[0 1],'ButtonDownFcn',@startDragFcn);
set(f,'WindowButtonUpFcn',@stpDragFcn);
function startDragFcn(varargin)
set(f,'WindowButtonMotionFcn',@draggingFcn);
end
function draggingFcn(varargin)
pt = get(aH,'CurrentPoint');
set(h,'XData',pt(1)*[1,1]);
end
function stopDragFcn(varargin)
set(f,'WindowButtonMotionFcn','');
end
end
I can't click-drag-release; however, if I click and release, I can drag, but obviously can't stop (except if I click outside the plot area on the figure).
I tried without great hope 'hold all', to modify 'Interruptible'/'Hittest' properties, but nothing works and I'm out of ideas...
Thank you for your help

采纳的回答

Jeremy
Jeremy 2016-7-7
编辑:Jeremy 2016-7-7
Even though my solution is not really elegant, solved.
%added in the stardrag function:
set(linehandle,'HitTest','off');
%added in the stopdrag function:
set(linehandle,'HitTest','on');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by