Got Questions? Get Answers.
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:
Get mouse coordinates inside axes

Subject: Get mouse coordinates inside axes

From: Jan

Date: 29 Apr, 2010 12:12:04

Message: 1 of 4

Dear

Can anyone help me to get the coordinates of my mouse only inside an axes. I want to make an erose function with my mouse.

So when I push my button I got this code:

global hSeg;
global axesWidth; global axesHeight;
axesPositions= get(hSeg,'Outerposition'); % this don't work
axesWidth = axesPositions(1,3); % this don't work
axesHeight = axesPositions(1,4); % this don't work

hFig = get(0,'CurrentFigure');

set(hFig,'WindowButtonMotionFcn',@moviment);
set(hFig,'WindowButtonDownFcn',@moviment_down);

This code I got for my moviment function:
function moviment(hco,eventStruct)
global hSeg;
global axesWidth; global axesHeight;

p = get(hSeg,'CurrentPoint');
x = p(1,1);
y = p(1,2);

if x >= 0 && x <= axesWidth && y >= 0 && y <= axesHeight;
    x
    y
end

The problem is that I get the wrong results for axesWidth and axesHeight. This means that I can only select in a small part of the axes

Thanks in advance
Jan

Subject: Get mouse coordinates inside axes

From: Matt Fig

Date: 29 Apr, 2010 16:18:05

Message: 2 of 4

See Q#29
http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples

Subject: Get mouse coordinates inside axes

From: Camille Couzi

Date: 18 Aug, 2010 16:15:24

Message: 3 of 4

"Matt Fig" <spamanon@yahoo.com> wrote in message <hrcbft$382$1@fred.mathworks.com>...
> See Q#29
> http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples


Hi!!!!
How could I modify the script so that it works if I have all my figure, buttons, panel... defined in "units normalized"?
The result I want is the x y coordinates over my imagesc.
Thanks!!

Camille.

Subject: Get mouse coordinates inside axes

From: Jan

Date: 23 Aug, 2010 08:01:07

Message: 4 of 4

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4h0us$k0v$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hrcbft$382$1@fred.mathworks.com>...
> > See Q#29
> > http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
>
>
> Hi!!!!
> How could I modify the script so that it works if I have all my figure, buttons, panel... defined in "units normalized"?
> The result I want is the x y coordinates over my imagesc.
> Thanks!!
>
> Camille.

Hi

I used this method:
- I put this code behind a toggle button
    set(hFig,'WindowButtonMotionFcn',@moviment);
    set(hFig,'WindowButtonDownFcn',@moviment_down);
    set(hFig,'WindowButtonUpFcn',@moviment_up);

- Function movement:
    p = get(hSeg,'CurrentPoint');
    xPixel = p(1,1);
    yPixel = p(1,2);
    % We need image width and height due to the different measurements and
    % fixed size of axes.
    if xPixel >= 0 && xPixel <= imWidth && yPixel >= 0 && yPixel <= imHeight;
        set(gcf,'Pointer','cross');
        if MouseDown == 1
            if ySegmented(round(yPixel),round(xPixel)) == 1
            ySegmented(round(yPixel),round(xPixel)) = 0;
            end

            axes(hSeg);
            reset(hSeg);
            imagesc(ySegmented);
            axis image;
            axis off;
        end
    else
        set(gcf,'Pointer','arrow');
    end

- Function movement_up:
   global MouseDown;

    % If mousebutton is released, pixels my not be changed
    MouseDown = 0;

- Function movement_down
MouseDown = 1;

% For all the togglebuttons there is an if-structure where they will add
% pixels or erase pixels from the segmented image
    p = get(hSeg,'CurrentPoint');
    xPixel = p(1,1);
    yPixel = p(1,2);
    if xPixel >= 0 && xPixel <= imWidth && yPixel >= 0 && yPixel <= imHeight;
        set(gcf,'Pointer','cross');
        if MouseDown == 1
            if ySegmented(round(yPixel),round(xPixel)) == 1
            ySegmented(round(yPixel),round(xPixel)) = 0;
            end
            
            axes(hSeg);
            reset(hSeg);
            imagesc(ySegmented);
            axis image;
            axis off;
        end
    else
        set(gcf,'Pointer','arrow');
    end


Is this clear to you?
Jan

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