How to receive cursor data from axes with background image in a GUI?

3 views (last 30 days)
Hi,
I am working with a GUIDE and want to use an axis element to enable the user selecting input data within a 2D Plane, just like this:
I used the axes create fcn to insert a background image into my axis.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
cbcr_plane = imread('input/cbcrplane.png');
hImage = imagesc(0:255,0:255,(flipdim(cbcr_plane,1)));
set(gca,'ydir','normal');
datacursormode on;
How can I access the current cursor position (X and Y) within my GUI? Thx in advance!

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Aug 2014
Daniel - how do you wish to have access to this information? Through another callback of some GUI widget? If so, you could do something like the following - assume that you when you push a button, you want to get the information from the last (or current) cursor position. In your above axes1_CreateFcn you could save the handle to the data cursor object to the handles structure so that it will be accessible to other callbacks
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
cbcr_plane = imread('input/cbcrplane.png');
hImage = imagesc(0:255,0:255,(flipdim(cbcr_plane,1)));
set(gca,'ydir','normal');
% create the data cursor object
handles.datacursor = datacursormode;
% enable the data cursor object
set(handles.datacursor,'Enable','on');
% save the object to the handles structure
guidata(hObject,handles);
Now suppose you have a push button (named pushbutton1) and that you want that whenever you press this button, the position of the current (or last) cursor position is printed out. In the callback to this button, do the following
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isfield(handles,'datacursor')
cursorInfo = getCursorInfo(handles.datacursor);
if ~isempty(cursorInfo)
fprintf('xpos=%f ypos=%f\n',cursorInfo.Position(1),cursorInfo.Position(2));
end
end
So whenever you press this button, the last/current cursor position (if it exists) will be written to the console.
Try the above and see what happens!

Categories

Find more on Graphics Object Programming 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!