Looking to get mouse position feedback interactively over a plot and send to text boxes in GUI
Afficher commentaires plus anciens
I have a figure in a GUI that is a map (lat/lon). I want to send that lat/long to 2 text boxes in the GUI as the mouse goes over the map interactively and then capture the final coordinate with a mouse click (using ginput). Here is part of my code...
function load_map_Callback(hObject, eventdata, handles)
% hObject handle to load_map (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
plotMapDTED_copper_gui(map,'deg',handles.axes1);
if(get(handles.option_mouse_click_linear,'Value'))
datacursormode on
%Enter target location
message = sprintf('Select the location on the map as the Target:\n\nClick OK to continue.\nClick Exit to exit this function');
button = questdlg(message, 'Enter Target', 'OK', 'Exit','OK');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'Exit')
reset2default(handles);
return;
end
if LocalMouseOverMap (hObject, handles)
set(hObject,'Pointer','crosshair');
cp = get(hObject,'CurrentPoint');
xxxx=cp(1,1);
yyyy=cp(1,2);
set(handles.text19,'string',num2str(xxxx,'%.3f'));
set(handles.text21,'string',num2str(yyyy,'%.3f'));
[xt yt] = ginput(1);
end
scatter(xt, yt,'MarkerFaceColor',[0 0 1],'Marker','*');
then at the end I had this call
function status = LocalMouseOverMap (hObject, handles)
% get the current mouse position
cp = get(hObject,'CurrentPoint');
% Get the position of the map axes in pixels
set(handles.axes1,'units','pixels');
pos = get(handles.axes1,'position');
set(handles.axes1,'units','normalized');
% Determine if the mouse position is with the axes boundaries
if (cp(1) > pos(1) && cp(1) < pos(1) + pos(3) ...
&& cp(2) > pos(2) && cp(2) < pos(2) + pos(4))
status = true;
else
status = false;
end
1 commentaire
Walter Roberson
le 1 Nov 2011
your code does not look unreasonable at first glance. Are you encountering a specific problem?
Réponses (2)
william gilliland
le 2 Nov 2011
0 votes
Robert Cumming
le 2 Nov 2011
Check what hObject actually is - I suspect its not the figure handle - and hence doesn't have the property "CurrentPoint".
If hObject is a child of figure (I would have thought so), you can get to the figure by going up through its parent.
e.g.
h = figure;
child = uicontrol ( 'parent', h, 'style', 'edit' )
get ( get ( child, 'parent' ), 'CurrentPoint' )
2 commentaires
Walter Roberson
le 2 Nov 2011
Or better than assuming you know the nesting level, use
get(ancestor(hObject,'figure'),'CurrentPoint')
Robert Cumming
le 2 Nov 2011
Very good.
you learn something new everyday....
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects 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!