How do I obtain the coordinates of the mouse on the screen displayed somewhere in my figure window?

R
rew_sahsani · Jan 30, 2024 · 2K views
Question
How do I obtain the coordinates of the mouse on the screen displayed somewhere in my figure window?
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 26, 2026
This enhancement has been incorporated in MATLAB 7.5 (R2007b).
 
As a workaround in previous MATLAB releases, you can use the following MATLAB function to track mouse motion:
 
 
function mousetrk
%MOUSETRK function to track mouse motion
% This function creates two static text controls
% which contain the x & y coordinates of the mouse
% The driving callback is executed by the
% WindowButtonMotionFcn of the figure.

figure
axes('Visible','off')

xbox = uicontrol('Style','text','Tag','xbox');
xboxlabel = uicontrol('Style','text','String','X Value',...
    'Position',get(xbox,'position')+[0 21 0 0]);

ybox = uicontrol('Style','text','Position',[90 20 60 20],'Tag','ybox');
yboxlabel = uicontrol('Style','text','String','Y Value',...
    'Position',get(ybox,'position')+[0 21 0 0]);

callback = ['pt = get(gca,''CurrentPoint'');'...
  'xbox = findobj(''Tag'',''xbox'');' ...
  'ybox = findobj(''Tag'',''ybox'');' ...
  'set(xbox,''String'',num2str(pt(1,1)));' ...
  'set(ybox,''String'',num2str(pt(1,2)));'];
set(gcf,'WindowButtonMotionFcn',callback);

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!