draw polygon continuously with mouse

5 views (last 30 days)
Amit Ifrach
Amit Ifrach on 30 Jul 2024
Edited: Piyush Kumar on 30 Jul 2024
לק"י
Hi guys,
I need to contour some cells. I want to use the drawpolygon command, or an equivalent of it, that will record mouse position continuously while mouse left click button is held. No such option seems to be in the drawpolygon documentation.
There are some old answers about how to do it, the problem is that most of the codes there somewhy don't work when I try to use them. Is there any other easy/quick way to obtain mouse position only when left mouse button is held?
Thanks,
Amit.

Answers (1)

Piyush Kumar
Piyush Kumar on 30 Jul 2024
Edited: Piyush Kumar on 30 Jul 2024
You can use "WindowButtonMotionFcn". Read more about this from "Window Callbacks" section of this documentation link.
Step 1: Create a file named "drawPolygon.m" with the following content -
function drawPolygon()
figure('WindowButtonMotionFcn', @mouseMove);
global isDown;
isDown = false;
set(gcf, 'WindowButtonDownFcn', @mouseDown);
set(gcf, 'WindowButtonUpFcn', @mouseUp);
end
function mouseMove(src, event)
global isDown;
if isDown
C = get(gca, 'CurrentPoint');
disp(['Cursor position is (' num2str(C(1,1)) ', ' num2str(C(1,2)) ')']);
end
end
function mouseDown(src, event)
global isDown;
isDown = true;
end
function mouseUp(src, event)
global isDown;
isDown = false;
end
Step 2: From MATLAB command line, run the file -
drawPolygon
Step 3: In the figure, you keep moving the cursor and you will get the current position of the cursor live in the MATLAB command line.
Let me know if this helps!

Community Treasure Hunt

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

Start Hunting!