Changing axes properties with the mouse

August 31, 2009 by Husam Aldahiyat 
Filed under: GUI, Tutorials 
Leave a Comment
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)

Today we’re going to learn how to change some axes properties with the mouse.

First, start a new m file function that will contain our work.

Husam1

Create the axes on which our work will be based on. Give the axes a handle (h) so we can change and obtain (set and get) its properties later.

Husam2

You’ll see that the handle variable h has an orange (warning) underline. This happens when your function contains an unused variable in it. We will use this handle soon in manipulating the axes.

To see the newly created axes, run the function.

Husam3

The first thing we will do with our axes is getting mouse click input. To do this, we first assign a buttondownfcn to our axes.

Husam4

Now whenever we click on the axes with the mouse, we go to the function press1 and perform whatever is instructed there.

We will at first put an acknowledgement of the mouse click. This is done by changing the title of the axes whenever we click on it with a small phrase. This phrase then disappears after half a second.

Husam5

Try it!

Next we will identify the coordinates of the mouse click. To do this, first delete the previous lines inside the press1 function. To get the mouse click coordinates, we will make use of an axes property called currentpoint.

So we take the currentpoint value in our axes and store the result in a variable, as follows.

Husam6

Of this new variable (which will be a 2 by 3 matrix), we are interested in the x and y coordinates, namely elements (1,1) and (1,2) of the matrix p.

Husam7

Now we have the x and y coordinates of the mouse click(s) stored in appropriate variable names. To show these values, we will change the title of the axes accordingly, as done before.

The x and y variables are numeric, while the title of the axes contains string values. For this reason, we will convert the numeric data into string data, and then put it as a title for the axes. For aesthetic reasons, we will also put a comma between the two values. This is shown in the following code snippet.

Husam8

Result:

Husam9

We can do more things with the mouse click coordinates. For example, the following code plots a star wherever you click the mouse on the axes. Since in MATLAB plotting a new plot replaces the old one, we will use the command hold on to make sure each star plotted stays on the axes.

Also, we set the axes limits beforehand so they stay the same and don’t change after every star plotted.

Husam10

Result:

Husam11

For our final example, we want to change the axes axis limits with mouse clicks. That is, when we click on the far right of the axes, the x-limit changes accordingly. The algorithm works as follows:

  • Get x and y coordinates for mouse click.
  • If x coordinate is in last 0.05 of the x-limit, shift x-limit right.
  • Similarly, if x coordinate is in first 0.05 of the x-limit, shift x-limit left.
  • Do the same for the axes y-limit.

The following is the code with comments.

Husam12

Thanks for tuning in J

VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
Changing axes properties with the mouse9.0101

Popularity: 3% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


Include MATLAB code in your comment by doing the following:

<pre lang="MATLAB">

%insert code here

</pre>