getmousepos
Returns the mouse position in the figure area.
Syntax
[x, y] = getmousepos()
getmousepos('print', status)
getmousepos('print', status, 'parent', handle)
Inputs
- status
- If the value is 'on', the mouse position will be printed in the command window, whenever there is a right mouse click in a figure that has only GUI elements and no axes. This property is useful when building a gui as both the normalized and pixel positions are printed. If the value is 'off', mouse position will not be printed on right click.
- handle
- Optional input which specifies the handle of a ui element that will be used as a parent (reference) when calculating the normalized position printed. If no parent property is given, the current <cmdname>gcf</cmdname> handle will be used as reference. If the mouse position is not contained in the parent handle, the normalized position will be [NaN NaN] in the message printed.
Outputs
- x, y
- The position of the mouse.
Examples
In the following example, getmousepos is used along with the
'mouseclickcallback' property to obtain the mouse position everytime you
click on the
plot:
close all;
plot(rand(100,1));
set(gca, 'mouseclickcallback',@get_mouse_pos);
function get_mouse_pos(handle, callbackdata)
[x,y] = getmousepos()
callbackdata
end
The next example demonstrates the two coordinate systems. The mouse coordinates are printed
in the console as the mouse moves over the
figure.
close all;
figure();
subplot(1,2,2);
plot(rand(10,1));
ellipse(gcf,'pos',[0, 0, 20, 20], 'edgecolor', 'r','linewidth',2);
for i=1:100
[x,y] = getmousepos()
pause(1);
end
getmousepos and the 'mouseclickcallback' property are
combined in the following example to create new elements in the
figure:
clear all, close all;
axes('position',[0,0,0,0]);
set(gca,'mouseclickcallback',@mouseclicked);
function mouseclicked(h,callbackdata)
[x,y]=getmousepos();
ellipse(gcf,'pos',[x-10, y-10, 20, 20])
end
getmousepos with printing in a figure used for GUI elements only
getmousepos('print', 'on')
f = gcf();
% Right click in the figure once it is created. The following message will be printed
Mouse position: normalized [0.45 0.44], pixels [307 143], parent [1.000000]
getmousepos('print', 'off')
getmousepos with printing in a figure used for GUI elements only, with a parent reference
getmousepos('print', 'on')
f = gcf();
% Right click in the figure once it is created. The following message will be printed
Mouse position: normalized [0.45 0.44], pixels [307 143], parent [1.000000]
% Use the normalized mouse position to create a frame inside gcf
frame1 = uipanel(gcf(), 'title', 'Frame1', 'units', 'normalized', 'position', [0.45 0.44 0.5 0.4]);
getmousepos('print', 'on', 'parent', frame1)
% Right click in the parent frame to get the position where a button needs to be created
Mouse position: normalized [0.12 0.38], pixels [348 193], parent [17.819334]
% Use the normalized mouse position to create a button inside parent object
button = uicontrol(frame1, 'style', 'pushbutton', 'string', 'Next', 'units', 'normalized', 'position', [0.12 0.38 0.25 0.18]);
getmousepos('print', 'off')
Comments
If the mouse is not inside the figure area when getmousepos is called, the outputs will be NaN.