I am trying to create a callback function(s) that does the following:
I think I have to use "impoint" to generate the draggable points and to use "bsxfun" for the snapping. I have something in the similar context that uses "imline" and the functions are provided below if it's of any help.
function calc_slope(handle,event)
axis_h = findobj(gcf,'Type','axes');
line = get(gco);
xdata = line.XData;
ydata = line.YData;
tb_h = text(0,0,'');
fcn_constr = @(pos) imline_snap(pos, [xdata(:) ydata(:)],tb_h);
imline_h = imline(axis_h, 'PositionConstraintFcn', fcn_constr);
addlistener(imline_h, 'ObjectBeingDestroyed', @(obj,event) delete(tb_h));
function constr_pos = imline_snap(new_pos, positions, tb_h)
[~, ind1] = min(sum(bsxfun(@minus, new_pos(1,:), positions).^2, 2));
[~, ind2] = min(sum(bsxfun(@minus, new_pos(2,:), positions).^2, 2));
constr_pos = [positions(ind1,:); positions(ind2,:)];
set(tb_h, 'String',...
sprintf([' \\DeltaY/\\DeltaX = ',...
num2str((constr_pos(2,2)-constr_pos(1,2))/(constr_pos(2,1)-constr_pos(1,1))),...
'\n \\DeltaX = ',num2str(constr_pos(2,1)-constr_pos(1,1)),...
', \\DeltaY = ',num2str(constr_pos(2,2)-constr_pos(1,2))]),...
'Position', mean(constr_pos));
The core functions were written by @Luis Mendo and even now I can't understand how all these work: all credits to him. Can someone modify the script or create from scratch to accomplish the problem described above?
I resorted to using datacursormode and getCursorInfo. No lines or automatic updating, but points are snapped and draggable.