Axes with moving vertical line

15 views (last 30 days)
peetman
peetman on 14 Jul 2016
Commented: Cedric Bareille on 5 Mar 2021
I would like to add two vertical moving lines to the graph and depending on their position it would change the values in the boxes Start and End.
I would also like to do the other way around: by changing the values in the boxes Start and End it would move the vertical lines to the assigned positions.
The lines would be moved by a mouse event.
I have this inside the pushbutton1_Callback
hold on;
plot(x,y);
SP = 20;
line([SP,SP],get(handles.axes1,'Xlim'),'Color','red');
I suppose I would have to create callback events for mouseButtonDown and mouseButtonUp, but I am very new to Matlab and don't know what to put in those callbacks

Answers (1)

Geoff Hayes
Geoff Hayes on 20 Jul 2016
peetman - you will need to include callbacks for the mouse button down, motion, and up events.
set(hFig,'WindowButtonDownFcn', @mouseDown);
set(hFig,'WindowButtonMotionFcn',@mouseMove);
set(hFig,'WindowButtonUpFcn', @mouseUp);
where hFig is the GUI. This way you will be able to capture when the user presses the mouse button down to see if the cursor is near one of your vertical lines, when the user moves the cursor, and when the user releases the button (to end moving the line).
If you save the handles to the lines that you have drawn, then you will be able to update their position as you move the cursor.
See attached for an example.
  1 Comment
Cedric Bareille
Cedric Bareille on 5 Mar 2021
Thanks so much ! This is a very helpful example !

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!