How can i use the function getline and see the position of cursor?

1 view (last 30 days)
How can i use the function getline ( [area.x area.y]=getline(gcf,'closed');) and at the same time see the position of cursor? I see this example but i dont know where i put the function getline.
% main function function main_fcn
% create figure
hFigure = figure;
% make sure figure is drawn before properties are set
drawnow
% create a text box ui control to display mouse position
hText = uicontrol(hFigure, 'Style', 'text', ...
'BackgroundColor', [0 0 0], ...
'ForegroundColor', [1 1 1]);
% set properties of figure and define mouse motion callback function
set( hFigure, ...
'color', [0 0 0], ...
'menubar', 'none', ...
'toolbar', 'none', ...
'name', 'Graphics Output Window', ...
'numbertitle', 'off', ...
'pointer', 'crosshair', ...
'WindowButtonMotionFcn', {@hFigure_MotionFcn, hText});
% mouse motion callback function
function hFigure_MotionFcn(hFigure, event_data, hText)
% get position of mouse
mouse_pos = get(hFigure,'CurrentPoint');
set( hText, ...
'Position', [mouse_pos(1) mouse_pos(2) 100 20], ...
'String', ['x = ' num2str(mouse_pos(1)) ', y = ' num2str(mouse_pos(2))]);

Answers (1)

Prateekshya
Prateekshya on 17 Oct 2024
Hello Pedro,
To integrate the getline function with your existing MATLAB script and display the cursor position simultaneously, you will need to manage the event callbacks carefully. The challenge is that getline captures the mouse input for drawing a line or polygon, so you need a way to update the cursor position without interfering with getline. Here is how you can achieve this:
  1. Use a Timer: You can use a MATLAB timer to periodically update the cursor position in the text box while getline is active.
  2. Update the Cursor Position: Modify the mouse motion callback to update the text box with the cursor position.
To know more about timer, please follow this link: https://www.mathworks.com/help/matlab/ref/timer.html
I hope this helps!

Community Treasure Hunt

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

Start Hunting!