draw polygon continuously with mouse

4 次查看(过去 30 天)
לק"י
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.

回答(1 个)

Piyush Kumar
Piyush Kumar 2024-7-30
编辑:Piyush Kumar 2024-7-30
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!

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by