Getting positions without clicking on plot with ginput command on MATLAB

676 views Asked by At

I am trying to get positions at the following map with ginput command. But the problem is I want to see the position of the point before clicking it.

Is it possible to that? After I clicked N points I can see positions, but I can not click them anymore. I should see the position first and after that I need to click it.

Thanks in advance!

Here is the code:

clc
clear
close all
geoaxes('Units','normalized');
N=5;
set (gcf, 'WindowButtonMotionFcn', @mouseMove);

for i=1:N

[lat,lon]=ginput(1)
hold on
geolimits('manual')
geoscatter(lat,lon,'filled','b')
end

set (gcf, 'WindowButtonMotionFcn', @mouseMove);

function mouseMove (object, eventdata)
C = get (gcf, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
end
2

There are 2 answers

0
David On BEST ANSWER

If you have the mapping toolbox you can use gcpmap to simplify this I think.

The main issue just requires a drawnow in the callback function. Then I used waitforbuttonpress and CurrentPoint to get the location of the click instead of ginput.

h = geoaxes('Units','normalized');
geolimits('manual')
set (gcf, 'WindowButtonMotionFcn', @(x,y) mouseMove(x,y,h));
hold on

N=5;
for i=1:N
    waitforbuttonpress;
    pt = h.CurrentPoint;
    lat = pt(1,1);
    lon = pt(1,2);
    geoscatter(lat,lon,'filled','b')
end
hold off


function mouseMove (~, ~, handle)
C = handle.CurrentPoint;
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
drawnow
end
0
matlabgui On

The problem your having is that the ginput function temporarily overrides some callbacks, one way to solve this is to use a listener to listen for the mouse press on the axes instead,

function myFunction
    % create a figure and an axes
    f = figure;
    ax = axes( 'parent', f );

    % give the axes a title
    t = title ( ax, '');
    % add a callback to update the title when the mouse is moving
    f.WindowButtonMotionFcn = @(a,b)updateTitle ( ax, t );
    % add a listener to the user clicking on the mouse
    addlistener ( ax, 'Hit', @(a,b)mousePress ( ax, t ) )
end
function updateTitle ( ax, t, str )
  % this function updates the title
  % 2 input args is from the mouse moving, the 3rd is only passed in
  %   when the mouoe button is pressed
  if nargin == 2; str = ''; end
  % get the current point of the axes
  cp  = ax.CurrentPoint(1,1:2);
  % check to see if its in the axes limits
  if cp(1) > ax.XLim(1) && cp(1) < ax.XLim(2) && ...
      cp(2) > ax.YLim(1) && cp(2) < ax.YLim(2)
    % update the string
    t.String = sprintf ( '%f,%f %s', cp, str );
  else
    % if ourside the limits tell the user
    t.String = 'Outside Axes';
  end
end
% this function is run when the mouse is pressed
function mousePress ( ax, t )
  updateTitle ( ax, t, '- Button Pressed' );
end

This will require some refactoring of your code, but its a powerful method and introduces you to listeners.

Image when the mouse is moving:

Image when the mouse is moving

Image when the mouse is pressed: Image when the mouse is pressed