Http://blog.sina.com.cn/s/blog_544233cf0100fdxd.html
Today, I googled whether MATLAB can track the movement of the mouse!
How does Matlab GUI respond to mouse events?
From: matlab programming and Application
These properties are attached to a graphic window, so they regulate the response commands for mouse events in this graphic window.
In general, we want the mouse to be pressed first and then moved to trigger the response command during the movement. To achieve this effect, we must do the following:
When the mouse button is pressed, set windowbuttonmotionfcn and windowbuttonupfcn.
When the mouse button is released, clear the windowbuttonmotionfcn and windowbuttonupfcn values.
Therefore, the response command corresponding to windowbuttonmotionfcn is called only when the mouse button is pressed and the mouse is moving.
The following is a simple example. In this example, a mouse event produces the following responses:
When you press the mouse, "mouse down!" appears in the MATLAB command window !」 .
When you press and move the mouse, Matlab will draw the mouse track in the graphic window and print "Mouse is moving!" in the Command window !」 And the mouse position at that time.
When the mouse is released, "Mouse up!" appears in the MATLAB command window !」 .
Code
Function tmouse (Action)
% Tmouse This example shows how to set mouse events with handle graphics (mouse
% Events) response command (callbacks)
If nargin = 0, Action = 'start'; End
Switch (Action)
% Enable the graphics window
Case 'start ',
Axis ([0 1 0 1]); % sets the chart axis range
Box on; % Add the image axis to the box
Title ('click and drag your mouse in this window! ');
% Set the response command to "tmouse down" when the mouse button is pressed 」
Set (GCF, 'windowbuttondownfcn', 'tmouse down ');
% Response command when the mouse button is pressed
Case 'lowdown ',
% Set the response command to "tmouse move" when the mouse moves 」
Set (GCF, 'windowbuttonmotionfcn ', 'tmouse move ');
% Set the response command to "tmouse up" when the mouse button is released 」
Set (GCF, 'windowbuttonupfcn', 'tmouse up ');
% Print "mouse down !」 Message
Fprintf ('mouse down! \ N ');
% Response command when the mouse moves
Case 'move ',
Currpt = get (GCA, 'currentpoint ');
X = currpt (1, 1 );
Y = currpt (1, 2 );
Line (X, Y, 'marker', '.', 'erasemode', 'xor ');
% Print "Mouse is moving !」 Message and mouse current location
Fprintf ('mouse is moving! Current location = (% G, % G) \ n', currpt (), currpt ));
% Response command when the mouse button is released
Case 'up ',
% Clear the response command when the mouse moves
Set (GCF, 'windowbuttonmotionfcn ','');
% Clear the response command when the mouse button is released
Set (GCF, 'windowbuttonupfcn ','');
% Print "Mouse up !」 Message
Fprintf ('mouse up! \ N ');
End