Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reading mouse position while the mouse is moving in GUI

Subject: Reading mouse position while the mouse is moving in GUI

From: Riikka

Date: 12 Aug, 2010 05:47:08

Message: 1 of 9

http://www.mathworks.com/support/solutions/en/data/1-1V97EZ/?solution=1-1V97EZ

The solution behind the link above is what I want to do, but I don't manage to do this in my GUI. I have one axis and I would like the GUI to show the current position of the mouse in a text box.

In addition to this, i would like there to be an option to save coordinates by a mouse click when wanted. How is this done?

Thanks for your help!

Subject: Reading mouse position while the mouse is moving in GUI

From: Robert

Date: 12 Aug, 2010 21:22:04

Message: 2 of 9

"Riikka " <nurminen_riikka@hotmail.com> wrote in message <i401ss$ag8$1@fred.mathworks.com>...
> http://www.mathworks.com/support/solutions/en/data/1-1V97EZ/?solution=1-1V97EZ
>
> The solution behind the link above is what I want to do, but I don't manage to do this in my GUI. I have one axis and I would like the GUI to show the current position of the mouse in a text box.
>
> In addition to this, i would like there to be an option to save coordinates by a mouse click when wanted. How is this done?
>
> Thanks for your help!

1. give code
2. use ButtonDownFcn of graphics object

Cheers,

     Robert

Subject: Reading mouse position while the mouse is moving in GUI

From: Riikka

Date: 13 Aug, 2010 05:54:06

Message: 3 of 9

"Robert " <notrust@lummert.net> wrote in message <i41ols$2mo$1@fred.mathworks.com>...
> "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i401ss$ag8$1@fred.mathworks.com>...
> > http://www.mathworks.com/support/solutions/en/data/1-1V97EZ/?solution=1-1V97EZ
> >
> > The solution behind the link above is what I want to do, but I don't manage to do this in my GUI. I have one axis and I would like the GUI to show the current position of the mouse in a text box.
> >
> > In addition to this, i would like there to be an option to save coordinates by a mouse click when wanted. How is this done?
> >
> > Thanks for your help!
>
> 1. give code
> 2. use ButtonDownFcn of graphics object
>
> Cheers,
>
> Robert

Thanks Robert. Unfortunately I can't give the code, because it doesn't exist yet. I managed to do part 1 with a figure outside my GUI just by using the code in the link above.

set (gcf, 'WindowButtonMotionFcn', @mouseMove);

function mouseMove (object, eventdata)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);

In which part of the GUI should I set the WindowButtonMotionFcn and ButtonDownFunction property to the figure in axis1? I probably just define the function mouseMove at the end of the GUI M-file, but I don't know where to put the first line (set...).

Subject: Reading mouse position while the mouse is moving in GUI

From: Riikka

Date: 13 Aug, 2010 07:23:04

Message: 4 of 9

I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.

Subject: Reading mouse position while the mouse is moving in GUI

From: Camille Couzi

Date: 17 Aug, 2010 14:23:20

Message: 5 of 9

"Riikka " <nurminen_riikka@hotmail.com> wrote in message <i42rso$adc$1@fred.mathworks.com>...
> I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.


Hi Rikka,

Could you help me since you have found a solution for your problem of mouse position?
I am trying to make the same sort of thing: I am working with imagesc that I plot on axes. My imagesc is made from matrix of data. I would need to get the position of the mouse while the mouse is over the imagesc, the problem is that I think Currentpoint only gives the position in pixels, and I would need to get the position in X Y on my matrix...
example: if my matrix is:
A= 1 2 3
      4 5 6
      7 8 9
imagesc(A);

and my mouse is over the "5" value on my imagesc, then I get: MousePosition=A(2,2)

Thank you in advance for your help.

Subject: Reading mouse position while the mouse is moving in GUI

From: Riikka

Date: 18 Aug, 2010 06:41:05

Message: 6 of 9

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4e60o$228$1@fred.mathworks.com>...
> "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i42rso$adc$1@fred.mathworks.com>...
> > I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.
>
>
> Hi Rikka,
>
> Could you help me since you have found a solution for your problem of mouse position?
> I am trying to make the same sort of thing: I am working with imagesc that I plot on axes. My imagesc is made from matrix of data. I would need to get the position of the mouse while the mouse is over the imagesc, the problem is that I think Currentpoint only gives the position in pixels, and I would need to get the position in X Y on my matrix...
> example: if my matrix is:
> A= 1 2 3
> 4 5 6
> 7 8 9
> imagesc(A);
>
> and my mouse is over the "5" value on my imagesc, then I get: MousePosition=A(2,2)
>
> Thank you in advance for your help.

Hi Camille!

If I understood your problem correctly, you want the origin to be in the lower left corner, and the y-values to grow when moving up and x-values to grow when moving right over the image. I'm not a professional in Matlab, so my solution might be a bit non-stylish, but this is what I would do:

% A is the matrix to be displayed
C = get(gcf,'CurrentPoint')
x = C(1,1);
y = size(A,1) - C(1,2);

So I would simply just use the mathematics and change the pixel values into x and y after using CurrentPoint. Is this what you wanted?

Subject: Reading mouse position while the mouse is moving in GUI

From: Camille Couzi

Date: 18 Aug, 2010 14:15:24

Message: 7 of 9

"Riikka " <nurminen_riikka@hotmail.com> wrote in message <i4fva1$nh4$1@fred.mathworks.com>...
> "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4e60o$228$1@fred.mathworks.com>...
> > "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i42rso$adc$1@fred.mathworks.com>...
> > > I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.
> >
> >
> > Hi Rikka,
> >
> > Could you help me since you have found a solution for your problem of mouse position?
> > I am trying to make the same sort of thing: I am working with imagesc that I plot on axes. My imagesc is made from matrix of data. I would need to get the position of the mouse while the mouse is over the imagesc, the problem is that I think Currentpoint only gives the position in pixels, and I would need to get the position in X Y on my matrix...
> > example: if my matrix is:
> > A= 1 2 3
> > 4 5 6
> > 7 8 9
> > imagesc(A);
> >
> > and my mouse is over the "5" value on my imagesc, then I get: MousePosition=A(2,2)
> >
> > Thank you in advance for your help.
>
> Hi Camille!
>
> If I understood your problem correctly, you want the origin to be in the lower left corner, and the y-values to grow when moving up and x-values to grow when moving right over the image. I'm not a professional in Matlab, so my solution might be a bit non-stylish, but this is what I would do:
>
> % A is the matrix to be displayed
> C = get(gcf,'CurrentPoint')
> x = C(1,1);
> y = size(A,1) - C(1,2);
>
> So I would simply just use the mathematics and change the pixel values into x and y after using CurrentPoint. Is this what you wanted?

Hi Rikka
Thank you for your reply.
I have tried to implement the figure that I wanted, to display the values of the position, and your advices helped me. It is what I wanted, but only in part:
I have a figure (that we are going to call fig1 here), which as 2 subplots, in which I am displaying my matrix as I explained you (with imagesc). I have created a button that contain the windowmotionbutton function, and I display the positions of the cursor (when it is on fig1) in a second figure (fig2).
That works ok, it gives me the position of the cursor everywhere on the fig1, but in fact I would like to have the coordinates of the cursor when it is on one of my two subplots... I don't know if I explain good, sorry my english is a little bit bad.

Apparently, CurrentPoint tool can only be definedfor figures, not for axes, ie you can't write:
C = get(axes.handle,'CurrentPoint'),
only:
C = get(figure.handle,'CurrentPoint')
...
Any idea of how I can get that?
Thanks a lot for your help.

Camille.

Subject: Reading mouse position while the mouse is moving in GUI

From: Riikka

Date: 19 Aug, 2010 05:07:03

Message: 8 of 9

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4gpts$49v$1@fred.mathworks.com>...
> "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i4fva1$nh4$1@fred.mathworks.com>...
> > "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4e60o$228$1@fred.mathworks.com>...
> > > "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i42rso$adc$1@fred.mathworks.com>...
> > > > I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.
> > >
> > >
> > > Hi Rikka,
> > >
> > > Could you help me since you have found a solution for your problem of mouse position?
> > > I am trying to make the same sort of thing: I am working with imagesc that I plot on axes. My imagesc is made from matrix of data. I would need to get the position of the mouse while the mouse is over the imagesc, the problem is that I think Currentpoint only gives the position in pixels, and I would need to get the position in X Y on my matrix...
> > > example: if my matrix is:
> > > A= 1 2 3
> > > 4 5 6
> > > 7 8 9
> > > imagesc(A);
> > >
> > > and my mouse is over the "5" value on my imagesc, then I get: MousePosition=A(2,2)
> > >
> > > Thank you in advance for your help.
> >
> > Hi Camille!
> >
> > If I understood your problem correctly, you want the origin to be in the lower left corner, and the y-values to grow when moving up and x-values to grow when moving right over the image. I'm not a professional in Matlab, so my solution might be a bit non-stylish, but this is what I would do:
> >
> > % A is the matrix to be displayed
> > C = get(gcf,'CurrentPoint')
> > x = C(1,1);
> > y = size(A,1) - C(1,2);
> >
> > So I would simply just use the mathematics and change the pixel values into x and y after using CurrentPoint. Is this what you wanted?
>
> Hi Rikka
> Thank you for your reply.
> I have tried to implement the figure that I wanted, to display the values of the position, and your advices helped me. It is what I wanted, but only in part:
> I have a figure (that we are going to call fig1 here), which as 2 subplots, in which I am displaying my matrix as I explained you (with imagesc). I have created a button that contain the windowmotionbutton function, and I display the positions of the cursor (when it is on fig1) in a second figure (fig2).
> That works ok, it gives me the position of the cursor everywhere on the fig1, but in fact I would like to have the coordinates of the cursor when it is on one of my two subplots... I don't know if I explain good, sorry my english is a little bit bad.
>
> Apparently, CurrentPoint tool can only be definedfor figures, not for axes, ie you can't write:
> C = get(axes.handle,'CurrentPoint'),
> only:
> C = get(figure.handle,'CurrentPoint')
> ...
> Any idea of how I can get that?
> Thanks a lot for your help.
>
> Camille.


Actually I was trying to do quite the same thing myself earlier and did not success. So now we need someone else to help us. My problem at the moment is basically that I would like the mouse position to be shown only when the cursor is on top of the axis, not in top of the whole GUI, which Matlab considers as figure. I guess that the same solution would help us both.

Riikka

Subject: Reading mouse position while the mouse is moving in GUI

From: Camille Couzi

Date: 20 Aug, 2010 14:42:04

Message: 9 of 9

"Riikka " <nurminen_riikka@hotmail.com> wrote in message <i4ie5n$mnn$1@fred.mathworks.com>...
> "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4gpts$49v$1@fred.mathworks.com>...
> > "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i4fva1$nh4$1@fred.mathworks.com>...
> > > "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4e60o$228$1@fred.mathworks.com>...
> > > > "Riikka " <nurminen_riikka@hotmail.com> wrote in message <i42rso$adc$1@fred.mathworks.com>...
> > > > > I found it out! The property must be defined right after plotting the image. Hope this helps someone who is struggling with the same problem.
> > > >
> > > >
> > > > Hi Rikka,
> > > >
> > > > Could you help me since you have found a solution for your problem of mouse position?
> > > > I am trying to make the same sort of thing: I am working with imagesc that I plot on axes. My imagesc is made from matrix of data. I would need to get the position of the mouse while the mouse is over the imagesc, the problem is that I think Currentpoint only gives the position in pixels, and I would need to get the position in X Y on my matrix...
> > > > example: if my matrix is:
> > > > A= 1 2 3
> > > > 4 5 6
> > > > 7 8 9
> > > > imagesc(A);
> > > >
> > > > and my mouse is over the "5" value on my imagesc, then I get: MousePosition=A(2,2)
> > > >
> > > > Thank you in advance for your help.
> > >
> > > Hi Camille!
> > >
> > > If I understood your problem correctly, you want the origin to be in the lower left corner, and the y-values to grow when moving up and x-values to grow when moving right over the image. I'm not a professional in Matlab, so my solution might be a bit non-stylish, but this is what I would do:
> > >
> > > % A is the matrix to be displayed
> > > C = get(gcf,'CurrentPoint')
> > > x = C(1,1);
> > > y = size(A,1) - C(1,2);
> > >
> > > So I would simply just use the mathematics and change the pixel values into x and y after using CurrentPoint. Is this what you wanted?
> >
> > Hi Rikka
> > Thank you for your reply.
> > I have tried to implement the figure that I wanted, to display the values of the position, and your advices helped me. It is what I wanted, but only in part:
> > I have a figure (that we are going to call fig1 here), which as 2 subplots, in which I am displaying my matrix as I explained you (with imagesc). I have created a button that contain the windowmotionbutton function, and I display the positions of the cursor (when it is on fig1) in a second figure (fig2).
> > That works ok, it gives me the position of the cursor everywhere on the fig1, but in fact I would like to have the coordinates of the cursor when it is on one of my two subplots... I don't know if I explain good, sorry my english is a little bit bad.
> >
> > Apparently, CurrentPoint tool can only be definedfor figures, not for axes, ie you can't write:
> > C = get(axes.handle,'CurrentPoint'),
> > only:
> > C = get(figure.handle,'CurrentPoint')
> > ...
> > Any idea of how I can get that?
> > Thanks a lot for your help.
> >
> > Camille.
>
>
> Actually I was trying to do quite the same thing myself earlier and did not success. So now we need someone else to help us. My problem at the moment is basically that I would like the mouse position to be shown only when the cursor is on top of the axis, not in top of the whole GUI, which Matlab considers as figure. I guess that the same solution would help us both.
>
> Riikka


Hi Rikka,
I am still searching for a solution, and I have found some interesting stuff ,look at:
- help for the axes2pix function
- the GUI_27 in the following webpage:
http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples

We keep in contact for more ideas...
Cheers from south of Chile!

Camille.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us