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:
how to set 'WindowButtonMotionFcn' on subplot???

Subject: how to set 'WindowButtonMotionFcn' on subplot???

From: Camille Couzi

Date: 18 Aug, 2010 14:49:07

Message: 1 of 5

Hi,
I am trying to get current point position of the cursor (without cliking) when it is over a figure.
Apparently, the better function to do that is WindowButtonMotionFcn, but I whant to get the Current Point only over a subplot which is in my figure, not all over the entire figure. BUT: WindowButtonMotionFcn can only be defined for figures, not for axes. So how can I tell matlab to search first on what axes my cursor is on (all without cliking), and then search the current point over this axe??

Thanks in advance for your help!

Camille.

Subject: how to set 'WindowButtonMotionFcn' on subplot???

From: Walter Roberson

Date: 18 Aug, 2010 15:36:00

Message: 2 of 5

Camille Couzi wrote:

> I am trying to get current point position of the cursor (without
> cliking) when it is over a figure. Apparently, the better function to do
> that is WindowButtonMotionFcn, but I whant to get the Current Point only
> over a subplot which is in my figure, not all over the entire figure.
> BUT: WindowButtonMotionFcn can only be defined for figures, not for
> axes. So how can I tell matlab to search first on what axes my cursor is
> on (all without cliking), and then search the current point over this axe??

You cannot do that. You can, however, have the WindowButtonMotionFcn
detect which axes is involved and then return without doing anything
further if you are not over the desired axes.

Subject: how to set 'WindowButtonMotionFcn' on subplot???

From: Camille Couzi

Date: 18 Aug, 2010 16:09:26

Message: 3 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <A5Tao.64169$Bh2.3146@newsfe04.iad>...
> Camille Couzi wrote:
>
> > I am trying to get current point position of the cursor (without
> > cliking) when it is over a figure. Apparently, the better function to do
> > that is WindowButtonMotionFcn, but I whant to get the Current Point only
> > over a subplot which is in my figure, not all over the entire figure.
> > BUT: WindowButtonMotionFcn can only be defined for figures, not for
> > axes. So how can I tell matlab to search first on what axes my cursor is
> > on (all without cliking), and then search the current point over this axe??
>
> You cannot do that. You can, however, have the WindowButtonMotionFcn
> detect which axes is involved and then return without doing anything
> further if you are not over the desired axes.

Hi Walter.
Thanks for your reply.
Sorry for the question, but how do you do that?

I have been trying with other tips, like the one presented in the GUI29 in http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
The problem is that it is complecated because the figure which contains my GUI is defined in pixels, and then all my buttons are defined in units normalized, so that doesn't work for me.
If you could give me more details about your suggestion it would be great.
Thanks you!

Subject: how to set 'WindowButtonMotionFcn' on subplot???

From: Walter Roberson

Date: 18 Aug, 2010 16:27:29

Message: 4 of 5

Camille Couzi wrote:

> I have been trying with other tips, like the one presented in the GUI29
> in
> http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
>
> The problem is that it is complecated because the figure which contains
> my GUI is defined in pixels, and then all my buttons are defined in
> units normalized, so that doesn't work for me.

You can find the position in pixels no matter what the defined units are.

oldunits = get(HGObject, 'Units');
set(HGObject, 'Units', 'pixels');
objectlocation = get(HGObject, 'Position');
set(HGObject, 'Units', oldunits);

I seem to recall once seeing reference to a routine that returned the
location in pixels -- the source showed that it pretty much just did the
above sequence of operations.

Subject: how to set 'WindowButtonMotionFcn' on subplot???

From: Camille Couzi

Date: 18 Aug, 2010 16:56:22

Message: 5 of 5

Walter Roberson <roberson@hushmail.com> wrote in message <SRTao.18354$EF1.1063@newsfe14.iad>...
> Camille Couzi wrote:
>
> > I have been trying with other tips, like the one presented in the GUI29
> > in
> > http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
> >
> > The problem is that it is complecated because the figure which contains
> > my GUI is defined in pixels, and then all my buttons are defined in
> > units normalized, so that doesn't work for me.
>
> You can find the position in pixels no matter what the defined units are.
>
> oldunits = get(HGObject, 'Units');
> set(HGObject, 'Units', 'pixels');
> objectlocation = get(HGObject, 'Position');
> set(HGObject, 'Units', oldunits);
>
> I seem to recall once seeing reference to a routine that returned the
> location in pixels -- the source showed that it pretty much just did the
> above sequence of operations.

Hi Walter,
Thanks for your answer, I didn't know that tip!
I tried it but I have still a little problem:

- my figure (which contains the GUI) is defined in pixels (for a question of prctical use of the gui in diferent computers):
h.fig=figure('position',[800 200 1000 750] ,'name','GUI',...
    'numbertitle','off','toolbar','figure');

- I have a panel defined in normalized:
h.panel_contourplots= uipanel('parent',h.fig,'position',[.22 .01 .77 .98]);

- ans inside this panel I have the subplots (also defined in normalized), lets give you here one of them:
h.cp1=subplot(5,3,[1 2 4 5],'Parent',h.panel_contourplots);
set(h.cp1,'position',[0.05 0.7 0.62 0.25]);

So the problem is that at the end, I want the position of the cursor when it is inside the h.cp1 axe, IN PIXELS (I am ploting imagesc of matrixs).


I have tried the following thing, in my windowmotionbuttonfcn:

F = get(h.fig,'currentpoint') %position of the cursor in the figure

oldunits_panel = get(h.panel_contourplots, 'Units'); %get the units of panel_contourplot (which is normalized)
set(h.panel_contourplots, 'Units', 'pixels'); %set it to pixel
pos_panel=get(h.panel_contourplots,'pos'); %take the position of the panel
set(h.panel_contourplots, 'Units', oldunits_panel); %re-place 'normalized' in the units.

%same thing for h.cp1:
oldunits_cp1 = get(h.cp1, 'Units');
set(h.cp1, 'Units', 'pixels');
pos_cp1=get(h.cp1,'pos'); %posicion of cp1 in the panel
set(h.cp1, 'Units', oldunits_cp1);


% % now I calculate the relative position of cp1 in the figure
pos_cp1_ok(1)=pos_panel(1)+pos_cp1(1)*pos_panel(3); % posicion x
pos_cp1_ok(2)=pos_panel(2)+pos_cp1(2)*pos_panel(4); % posicion y
pos_cp1_ok(3)=pos_panel(3)*pos_cp1(3); % largo x
pos_cp1_ok(4)=pos_panel(4)*pos_cp1(4); % largo y
pos_cp1_ok
xlm_cp1=get(h.cp1,'xlim'); %xmin y xmax de cp1 en la figura
ylm_cp1=get(h.cp1,'ylim'); %y lim de cp1 en la figura
DFX_cp1 = diff(xlm_cp1); % =xlm_cp1(2)-xlm_cp1(1) => diferencia de los limites
DFY_cp1 = diff(xlm_cp1);


%test in order to see if the mouse is on the h.cp1:
test1_cp1 = pos_cp1_ok(1) <= F(1) && F(1) <= pos_cp1_ok(1) + pos_cp1_ok(3);
test2_cp1 = pos_cp1_ok(2) <= F(2) && F(2) <= pos_cp1_ok(2) + pos_cp1_ok(4);


if test1_cp1 && test2_cp1
    % Calculate the position of the pixel of the cp1:
    Cx_cp1 = round(xlm_cp1(1) + (F(1)-pos_cp1_ok(1)).*(DFX_cp1/pos_cp1_ok(3)));
    Cy_cp1 = round(ylm_cp1(1) + (F(2)-pos_cp1_ok(2)).*(DFY_cp1/pos_cp1_ok(4)));
end


Hope this is comprehensible...
Thank you so much for your help.

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