How to move two vertical lines, just by moving a line with the mouse?

6 views (last 30 days)
Israel  Hernández
Israel Hernández on 15 Jul 2019
Commented: Adam Danz on 15 Jul 2019
I have two axes with a vertical line in each one, I manage to move with the mouse the first but I also want the second to move when I move the vertical line of the first axes.
I found this code, but I still can not extend it to coordinate the movement of vertical lines for two axes. The xline does reflect the movement, but it does not erase the previous line, so it creates a shading.
randX = mean(FechasT);
hVerticalLines = line(graf1,[randX,randX],get(graf1,'Ylim'),'Color','red');
hLineToDrag = [];
hLineToDrag2 = [];
function clicMouse(hObject,~)
% if EsteCursorEnManipulacion(hObject, graf1)
for k=1:length(hVerticalLines)
xVertLineCoord = get(hVerticalLines(k),'XData');
hLineToDrag = hVerticalLines(k);
function posActualMouse(~,~)
hLineToDrag = [];
hLineToDrag2 = [];
function moverMouse(hObject,~)
% is the mouse down event within the axes?
if ~isempty(hLineToDrag) && EsteCursorEnManipulacion(hObject, graf1)
currentPoint = get(graf1,'CurrentPoint');
x = currentPoint(2,1);
set(hLineToDrag, 'XData', [x x]);
% hLineToDrag2=line(graf2,[x,x],get(graf1,'Ylim'),'Color','red');
function [status]=EsteCursorEnManipulacion(hCursor,hControl)
status = false;
% consiguiendo la posición del mouse
xCursor=puntoActualFig(1,1)/posicion(1,3); % normalize
yCursor= puntoActualFig(1,2)/posicion(1,4); % normalize
%consiguiendo la posición de los axes dentro de la GUI
controlPos = get(hControl,'Position');
minx = controlPos(1);
miny = controlPos(2);
maxx = minx + controlPos(3);
maxy = miny + controlPos(4);
if xCursor >= minx && xCursor <= maxx && yCursor >= miny && yCursor <= maxy
status = true;
  1 Comment
Adam Danz
Adam Danz on 15 Jul 2019
When you move the first line, you need to store it's initial and final x coordinates so you can calculate the offset. Then you just need to apply the same offset to the second line.
For example, line #1 moved from x = 2 to x = -4 so it moved 6 units to the left. Line #2 is at x = 7 so you need to change the XData values to 1.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!