matlab - Three draggable points on a plot -
i trying create callback function(s) following:
- a user clicks on line
- each click results in small circle snapped nearest data point
- as new data point (a new circle) selected, line created between 2 points (eventually, total of 2 lines between 3 points)
- when third data point selected, text box appear calculation based on 3 sets of xy coordinates
- as user drags around of 3 points, calculation in text box updated
- i'd have many sets of 3 points , associated text box want on different sections of curve
i think have use "impoint" generate draggable points , use "bsxfun" snapping. have in similar context uses "imline" , functions provided below if it's of 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 written @luis mendo , can't understand how these work: credits him. can modify script or create scratch accomplish problem described above?
i resorted using datacursormode , getcursorinfo. no lines or automatic updating, points snapped , draggable.
Comments
Post a Comment