'ButtonDownFcn' not working on first click

1 view (last 30 days)
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

Accepted Answer

Jeremy
Jeremy on 7 Jul 2016
Edited: Jeremy on 7 Jul 2016
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');

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!