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!