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

1 次查看(过去 30 天)
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 = [];
set(f2,'WindowButtonDownFcn',@clicMouse)
set(f2,'WindowButtonMotionFcn',@moverMouse)
set(f2,'WindowButtonUpFcn',@posActualMouse)
function clicMouse(hObject,~)
% if EsteCursorEnManipulacion(hObject, graf1)
posicionActual=get(graf1,'CurrentPoint');
posicionX=posicionActual(1,1);
for k=1:length(hVerticalLines)
xVertLineCoord = get(hVerticalLines(k),'XData');
hLineToDrag = hVerticalLines(k);
end
end
function posActualMouse(~,~)
hLineToDrag = [];
hLineToDrag2 = [];
end
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);
%y=currentPoint(2,2);
set(hLineToDrag, 'XData', [x x]);
% hLineToDrag2=line(graf2,[x,x],get(graf1,'Ylim'),'Color','red');
xline(graf2,x);
end
end
function [status]=EsteCursorEnManipulacion(hCursor,hControl)
status = false;
% consiguiendo la posición del mouse
puntoActualFig=get(hCursor,'CurrentPoint');
posicion=get(hCursor,'Position');
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;
end
end
  1 个评论
Adam Danz
Adam Danz 2019-7-15
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by