You GUI organization seems a bit chaotic. You have to be careful where you place the extra code in the .m
file generated by the GUIDE.
I suggest you replace all the code you placed in the middle of the function by a call to your own initialization function:
myGuiInitialization(handles) ;
Then, in the same file (at the bottom), add the following code:
function myGuiInitialization(handles)
% path = uigetimagefile; %// uncomment that to retrieve your intital functionality
% img = imread(path); %// uncomment that to retrieve your intital functionality
img = imread('peppers.png') ; %// Delete that to retrieve your intital functionality
himg = imshow(img, 'Parent',handles.axes1) ; %// get the handle of the image graphic object (needed to attach the datatip to it)
dcm_obj = datacursormode(handles.figure1); %// get the handle of the datacursor object
set(dcm_obj,'UpdateFcn', @myDatatipUpdateFcn ); %// assign it a callback
dcm_obj.createDatatip(himg) ; %// create a new DATATIP and attach it to the image object
This will load an image, create a datatip and associate a callback to it.
Your separate function managing the datatip update becomes:
function txt = myDatatipUpdateFcn(hobj, event_obj)
handles = guidata( event_obj.Target ) ; %// retrieve the collection of GUI graphic object handles
pointPosition = event_obj.Position; %// get the position of the datatip
%// Store the position in the figure "appdata", named "pointPosition"
setappdata( handles.figure1 , 'pointPosition' , pointPosition )
%// now we've saved the position in the appdata, you can also display it
%// on the datatip itself
txt = {'Point to Compute: ';...
['X:',num2str(pointPosition(1))]; ...
['Y:',num2str(pointPosition(2))] } ;
Now every time the datatip will be moved, the last point position will be saved in the figure AppData. I recommend you use the function setappdata
and getappdata
instead of the old userdata
(it's a thing of the past, avoid it if you can).
Specially, the way you were doing it, assigning the userdata
of the object 0
is not recommeded, because that will be stored in the main root object. If you close your gui these data may still exist. The setappdata
will store data attached to your GUI, which is a much cleaner way (when your gui is closed the data disappear with your gui).
Now when you want to do something with your point position saved in your AppData, you can just retrieve them with getappdata
.
For the purpose of this example, I created a pushbutton on the figure. When pushed, it will retrieve the point position and display it in the textbox.
When you create the pushbutton, the GUIDE will create an empty callback for it in the gui main function. Place the following code in it:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%// retrieve the point position saved in the figure appdata named "pointPosition"
pointPosition = getappdata( handles.figure1 , 'pointPosition' ) ;
%// that's it, you have your point position, now do whatever you need with it
%// prepare a test string with the position and display it in the label
txtstr = sprintf('Point to compute: X=%d , Y=%d',pointPosition) ;
set(handles.txt1,'String',txtstr);
This code will produce something like that:
The datatip label will update position at every move, but the text label at the bottom will only be updated when you press the pushbutton.