Получение позиций без нажатия на график с помощью команды ginput на MATLAB - PullRequest
0 голосов
/ 22 января 2020

Я пытаюсь получить позиции на следующей карте с помощью команды ginput. Но проблема в том, что я хочу увидеть положение точки, прежде чем щелкнуть по ней.

Возможно ли это? После того, как я нажал N точек, я могу видеть позиции, но больше не могу их щелкать. Сначала я должен увидеть позицию, а после этого мне нужно нажать на нее.

Заранее спасибо!

Вот код:

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 ]

1 голос
/ 22 января 2020

Проблема, с которой вы столкнулись, заключается в том, что функция ginput временно перекрывает некоторые обратные вызовы. Один из способов решения этой проблемы - использовать listener вместо прослушивания нажатия мыши на осях,

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

Это потребует некоторого рефакторинга вашего кода, но это мощный метод, который знакомит вас со слушателями.

Изображение при движении мыши:

Image when the mouse is moving

Изображение при нажатии мыши: Image when the mouse is pressed

1 голос
/ 22 января 2020

Если у вас есть набор инструментов отображения, вы можете использовать gcpmap, чтобы упростить это, я думаю.

Основная проблема просто требует drawnow в функции обратного вызова. Затем я использовал waitforbuttonpress и CurrentPoint, чтобы получить местоположение щелчка вместо 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
...