Getting more information from addNewPosi​tionCallba​ck()?

Hi,
I am using impoint() to drag and drop multiple points on a figure. Whenever a point is moved a function is supposed to be called. (the function stores the coordinates of the point in an array somewhere). addNewPositionCallback() works fine, the function gets called, but I have no way to tell which point has moved - the only inputs the function gets are the point's coordinates. There are several points in the figure at once and I need to know which one has moved. Is there anyway to return a handle to the point, or something similar, using addNewPositionCallback?
Example code or any other help is great :) Thanks!

3 Comments

JM
JM on 11 Jul 2012
Edited: JM on 11 Jul 2012
Does 'Good question' precede a 'Good answer'.... or does it mean the question is too good for an answer? :)
Not sure if it'll be a good answer, but it'll be an answer!

Sign in to comment.

 Accepted Answer

Major Edit
function playing_with_impoint
imshow('cameraman.tif');
h(1) = impoint;
h(2) = impoint;
addNewPositionCallback(h(1),@mytestcb)
addNewPositionCallback(h(2),@mytestcb)
x(1) = get(h(1),'UIContextMenu');
x(2) = get(h(2),'UIContextMenu');
function mytestcb(pos)
get(gco,'uicontextmenu')==x
end
end
This has to be on the list of cludgiest things I've done but here goes. The IMPOINT and the current object share a 'UIContextMen'. They can thus be linked this way.
Enjoy!

2 Comments

JM
JM on 11 Jul 2012
Edited: JM on 12 Jul 2012
Seems to be the right track... it displays a double like 174.0168 and each point has a unique double. But how do you tell which number goes with which point (like if the points are in an array as pointObj(1), pointObj(2), and pointObj(3), for example)?
Ok, the major edit looks good!
Another way I found to solve it: I used the matrix structure containing the points to associate each point with it's respective double. The during the callback, check to see which point matches the double returned by gco. Maybe not the best but it works!

Sign in to comment.

More Answers (1)

A little more elegant way is to use an anonymous function, and then you could pass anything, a natural thing to pass would be the imroi handle. Here is an example:
imshow('cameraman.tif');
h(1) = impoint;
h(2) = impoint;
Defined in mycb.m:
function mycb(h,pos,point_number)
% imroi callback function, first input is handle of calling imroi object,
% second is the updated position of the object, third is the point number
% an integer identifying which point called this callback
% ... insert your code here, using the handle, position, or id number
end % end of callback function
Back in the command window (or a calling script or function):
% Define anonymous functions, providing each with a handle and an id
mycb1 = @(pos) mycb(h(1),pos,1);
mycb2 = @(pos) mycb(h(2),pos,2);
% ... etc for any other points
addNewPositionCallback(h(1),@mycb1)
addNewPositionCallback(h(2),@mycb2)
Now, when a point's position changes, it calls a function which knows the roi handle, the new position, and an id which you have assigned to that point.
I realize this was an old question, but I came across it today and wanted to share a better way for future searchers.

5 Comments

I realise this is over a year later, but I was looking to do something similar and gave your solution a whack.
I'm using Matlab R2014A, and your solution is causing an error:
"mycb1" was previously used as a variable, conflicting with
its use here as the name of a function or command.
I copied the first and third code blocks verbatim into a script (test.m) and the second into a function (mycb.m). Hopefully you'll see this and have a handy solution, but considering how unlikely that is I'll probably go with the first answer given
mycb1 = @(pos) mycb(h(1),pos,1);
mycb2 = @(pos) mycb(h(2),pos,2);
% ... etc for any other points
addNewPositionCallback(h(1),mycb1)
addNewPositionCallback(h(2),mycb2)
Brilliant, thanks a million. I may be kicking myself for not thinking of this myself.
Guojin Feng comments to me:
Correct method.

I would like to share what I have achieved in the MATLAB GUI,

first initialize the variables when opening the program

%initialize variables
global p Polos r Raiz;
p=0;
Polos=[];

in an axis with context menu, I put an option to add a point and I save it in a global array and then use it.

% --------------------------------------------------------------------
function CM_add_Pol_Callback(hObject, eventdata, handles)
global p Polos POL;
%Accountant
p=p+1;
%add impoint
POL=impoint;
%Customize impoint
setString(POL,"p"+p);
setColor(POL,'r');
%Add to the array
Polos=[Polos;POL];
%Link function of the Impoint to a general
Funp = @(pos) Funpx(hObject, eventdata, handles,p);
%Create event change of impoint position
 addNewPositionCallback(Polos(p),Funp)

then I show the properties of the selected impoint in a static text

   % --------------------------------------------------------------------
  function Funpx(hObject, eventdata, handles,point_number)
  global Polos;
  %Read new position of the impoint
  position=getPosition(Polos(point_number));
  %Show changes
  set(handles.text2,'String',"NUM"+point_number+"pos "+"x"+position(1)+"y"+position(2))

Sign in to comment.

Categories

Asked:

JM
on 11 Jul 2012

Commented:

on 26 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!