gpt4 book ai didi

matlab - 加速 impoint 对象的创建

转载 作者:行者123 更新时间:2023-12-02 03:24:15 25 4
gpt4 key购买 nike

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

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

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

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

这个想法只是为用户提供纠正先前由 Matlab 计算的图形位置的可能性,这里以随机数为例。

最佳答案

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

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

enter image description here


根据您更新绘图的方式,您可以通过使用类似 clf 的函数获得总体加速。 (清晰的数字)和cla (清除轴)而不是总是打开一个新的图形窗口,如 this answer 中所述可能有用。


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

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 = {};

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

enter image description here

也许你能找到错误。

关于matlab - 加速 impoint 对象的创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com