Matlab GUI mouse event

Source: Internet
Author: User

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

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.