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:
Viewing curosr position over an image in pixels (or coordinates)

Subject: Viewing curosr position over an image in pixels (or coordinates)

From: Camille Couzi

Date: 20 Aug, 2010 13:25:24

Message: 1 of 2

Hi matlabers!

I am constructing a Gui which displays a matrix in an imagesc, and I am trying to get cursor position over this imagesc. For that, I am using the WindowMotionButtonFcn, and I have been looking at the Gui example in the following page:
http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
(the GUI_27).

Here is a simplified code of what I am doing (you can copy paste and run it in your matlab):

////------ BEGINNING OF THE CODE----//////

function test_move

h.fig=figure;
% Axe which contains the imagesc:
h.hax = axes('units','normalized','position',[0.1 .45 0.8 .5]);

%buttons for displaying the positions of the cursor:
h.X_text = uicontrol('parent',h.fig,'Style','text',...
                'Units','normalized','Position',[.08 .25 .1 .1],'string','X:');
h.X_valor = uicontrol('parent',h.fig,'Style','text',...
                'Units','normalized','Position',[.5 .25 .1 .1]);
h.Y_text = uicontrol('parent',h.fig,'Style','text',...
                'Units','normalized','Position',[.08 .1 .1 .1],'string','Y:');
h.Y_valor = uicontrol('parent',h.fig,'Style','text',...
                'Units','normalized','Position',[.5 .1 .1 .1]);

A=[ 5 6 4 8; 8 12 23 31;15 9 27 5];

colormap(jet(60));
imagesc(A);

% we call the windowbuttonmotionfcn:
set (h.fig, 'WindowButtonMotionFcn', {@MouseMove,h});


function MouseMove (hObject, eventdata,h)

%get the position of the mouse:
F = get(h.fig,'currentpoint');

%change axes position in pixels:
h.pos_hax=get(h.hax,'pos');
oldunits_hax = get(h.hax, 'Units');
set(h.hax, 'Units', 'pixels');
pos_hax_pix=get(h.hax,'pos'); %posicion de cp1 en el panel
set(h.hax,'Units', oldunits_hax);
set(h.hax,'pos',h.pos_hax);

xlm=get(h.hax,'xlim');
ylm=get(h.hax,'ylim');
DFX = diff(xlm);
DFY = diff(ylm);

% look if the mouse is inside the axe
test1 = pos_hax_pix(1) <= F(1) && F(1) <= pos_hax_pix(1) + pos_hax_pix(3);
test2 = pos_hax_pix(2) <= F(2) && F(2) <= pos_hax_pix(2) + pos_hax_pix(4);


if test1 && test2
    % calculates position of mouse on the axe:
    Cx= floor(xlm(1) + (F(1)-pos_hax_pix(1)).*(DFX/pos_hax_pix(3)));
    Cy = floor(ylm(1) + (F(2)-pos_hax_pix(2)).*(DFY/pos_hax_pix(4)));
    set(h.X_valor,'string',num2str(Cx));
    set(h.Y_valor,'string',num2str(Cy));
end
////------ END OF THE CODE----//////



I have two problems:

1. In my example I have the axes position in NORMALIZED (not pix like in the GUI_27), That's why I make a little tranformation in order to get results in pixels... But if you move the mouse over the imagesc, you will see that there is a problem, within the same square, the function gives two different coordinates, that is to say: there is a mistake somewhere, and in consequence a square is not one pixel, but more than one pixel.

2. If you move the mouse over the axis, the positions varies, How do I make to tell matlab that I want pixel information only within the imagesc (not the axis labels and ticks?)

THANKS A LOT FOR YOUR HELP!!

Subject: Viewing curosr position over an image in pixels (or coordinates)

From: Yair Altman

Date: 21 Aug, 2010 18:11:04

Message: 2 of 2

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4lvo4$941$1@fred.mathworks.com>...
> Hi matlabers!
>
> I am constructing a Gui which displays a matrix in an imagesc, and I am trying to get cursor position over this imagesc. For that, I am using the WindowMotionButtonFcn, and I have been looking at the Gui example in the following page:


Try http://www.mathworks.com/matlabcentral/fileexchange/16181-datamatrix

Yair Altman
http://UndocumentedMatlab.com

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