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

1 Ansicht (letzte 30 Tage)
JM
JM am 11 Jul. 2012
Kommentiert: Fabio Guerra am 26 Nov. 2017
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 Kommentare
JM
JM am 11 Jul. 2012
Bearbeitet: JM am 11 Jul. 2012
Does 'Good question' precede a 'Good answer'.... or does it mean the question is too good for an answer? :)
Sean de Wolski
Sean de Wolski am 11 Jul. 2012
Not sure if it'll be a good answer, but it'll be an answer!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 11 Jul. 2012
Bearbeitet: Sean de Wolski am 12 Jul. 2012
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 Kommentare
JM
JM am 11 Jul. 2012
Bearbeitet: JM am 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mike Bindschadler
Mike Bindschadler am 19 Sep. 2014
Bearbeitet: Mike Bindschadler am 19 Sep. 2014
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 Kommentare
Walter Roberson
Walter Roberson am 7 Nov. 2016
Guojin Feng comments to me:
Correct method.
Fabio Guerra
Fabio Guerra am 26 Nov. 2017

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))

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by