加速 impoint 对象的创建

Speed up creation of impoint objects

我必须在轴上创建一些可拖动的点。然而,这似乎是一个非常缓慢的过程,在我的机器上这样做需要一秒多一点:

x = rand(100,1);
y = rand(100,1);

tic;
for i = 1:100
    h(i) = impoint(gca, x(i), y(i));
end
toc;

任何关于加速的想法将不胜感激。

这个想法只是为了让用户能够纠正先前由 Matlab 计算的图形中的位置,这里以随机数为例。

您可以在 while 循环中使用 ginput 光标来标记要编辑的所有点。之后只需点击轴外离开循环,移动点并使用任意键接受。

f = figure(1);
scatter(x,y);
ax = gca;
i = 1;
 while 1
        [u,v] = ginput(1);
        if ~inpolygon(u,v,ax.XLim,ax.YLim); break; end;
        [~, ind] = min(hypot(x-u,y-v));
        h(i).handle = impoint(gca, x(ind), y(ind));
        h(i).index  = ind;
        i = i + 1;
 end


根据您更新绘图的方式,您可以通过使用像 clf (clear figure) and cla (clear axes) instead of always opening a new figure window as explained in 这样的函数来获得一般加速,这可能很有用。


或者,以下是我在评论中的意思的一个非常粗略的想法。它会抛出各种错误,我现在没有时间调试它。但也许它有助于作为一个起点。

1) 数据的常规绘图和datacursormode的激活

x = rand(100,1);
y = rand(100,1);
xlim([0 1]); ylim([0 1])

f = figure(1)
scatter(x,y)

datacursormode on
dcm = datacursormode(f);
set(dcm,'DisplayStyle','datatip','Enable','on','UpdateFcn',@customUpdateFunction)

2) 自定义更新函数评估所选数据提示并创建 impoint

function txt = customUpdateFunction(empt,event_obj)

pos = get(event_obj,'Position');
ax = get(event_obj.Target,'parent');
sc = get(ax,'children');

x = sc.XData;
y = sc.YData;
mask = x == pos(1) & y == pos(2);
x(mask) = NaN;
y(mask) = NaN;
set(sc, 'XData', x, 'YData', y);
set(datacursormode(gcf),'Enable','off')

impoint(ax, pos(1),pos(2));
delete(findall(ax,'Type','hggroup','HandleVisibility','off'));

txt = {};

它适用于,如果您只想移动一个点。重新激活数据光标模式并设置第二个点失败:

也许你能找到错误。