Getting more information from addNewPositionCallback()?
1 view (last 30 days)
Show older comments
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
Accepted Answer
Sean de Wolski
on 11 Jul 2012
Edited: Sean de Wolski
on 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 Comments
More Answers (1)
Mike Bindschadler
on 19 Sep 2014
Edited: Mike Bindschadler
on 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 Comments
Fabio Guerra
on 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))
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!