Getting more information from addNewPositionCallback()?
Afficher commentaires plus anciens
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 commentaires
Sean de Wolski
le 11 Juil 2012
Good question.
Sean de Wolski
le 11 Juil 2012
Not sure if it'll be a good answer, but it'll be an answer!
Réponse acceptée
Plus de réponses (1)
Mike Bindschadler
le 19 Sep 2014
Modifié(e) : Mike Bindschadler
le 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 commentaires
EOC
le 25 Nov 2015
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
Walter Roberson
le 25 Nov 2015
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)
EOC
le 26 Nov 2015
Brilliant, thanks a million. I may be kicking myself for not thinking of this myself.
Walter Roberson
le 7 Nov 2016
Guojin Feng comments to me:
Correct method.
Fabio Guerra
le 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))
Catégories
En savoir plus sur Desktop dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!