getmousepos
Returns the mouse position in the figure area.
Syntax
[x, y] = getmousepos()
Outputs
- x, y
- The position of the mouse.
Examples
close all;
plot(rand(100,1));
set(gca, 'mouseclickcallback',@get_mouse_pos);
function get_mouse_pos(handle, callbackdata)
[x,y] = getmousepos()
callbackdata
end
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
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
Comments
If the mouse is not inside the figure area when getmousepos is called, the outputs will be NaN.