如何在matlab中绘制当前图形上的线条
How to plot lines on current figure in matlab
我是 matlab 新手。我已经在 imagesc 函数中打开了一个矩阵。我想通过鼠标点击收集的点来绘制线条。
这是我目前拥有的
load('maze', 'A');
% adjust coordinate
hold on;
plot(1,1);
imagesc(A);
% get points from mouse
[x,y] = ginput;
% graph the line
line(x, y, 'Color', 'r');
现在,我想要实现的是实时绘图,而不是在完成所有点击之后。如果单击一次,您会在地图上看到一个点,如果单击两次,您会在地图上看到一条线。我该如何实现?
如果你写ginput(1)
,那么,只会记录一分。单击后,将绘制该线。要绘制折线,您需要在每次单击后绘制新线。您可以使用循环来完成这项工作:
A = zeros(100);
% adjust coordinate
hold on;
plot(1,1);
imagesc(A);
% get points from mouse
x = [];
y = [];
while true
[x1,y1] = ginput(1);
x = [x;x1];
y = [y;y1];
delete(h);
% graph the line
h = line(x, y, 'Color', 'r');
end
hold off
我使用了无限循环;您可能想使用最适合您目的的其他循环。
line(x, y, 'Color', 'r');
会在每次迭代后绘制一条额外的折线。单击 10 次后,您的图上将有 10 条多段线。为避免这种情况,我们给我们的线名称 h
并在将新折线放在图形上之前将其删除。 HTH
我是 matlab 新手。我已经在 imagesc 函数中打开了一个矩阵。我想通过鼠标点击收集的点来绘制线条。
这是我目前拥有的
load('maze', 'A');
% adjust coordinate
hold on;
plot(1,1);
imagesc(A);
% get points from mouse
[x,y] = ginput;
% graph the line
line(x, y, 'Color', 'r');
现在,我想要实现的是实时绘图,而不是在完成所有点击之后。如果单击一次,您会在地图上看到一个点,如果单击两次,您会在地图上看到一条线。我该如何实现?
如果你写ginput(1)
,那么,只会记录一分。单击后,将绘制该线。要绘制折线,您需要在每次单击后绘制新线。您可以使用循环来完成这项工作:
A = zeros(100);
% adjust coordinate
hold on;
plot(1,1);
imagesc(A);
% get points from mouse
x = [];
y = [];
while true
[x1,y1] = ginput(1);
x = [x;x1];
y = [y;y1];
delete(h);
% graph the line
h = line(x, y, 'Color', 'r');
end
hold off
我使用了无限循环;您可能想使用最适合您目的的其他循环。
line(x, y, 'Color', 'r');
会在每次迭代后绘制一条额外的折线。单击 10 次后,您的图上将有 10 条多段线。为避免这种情况,我们给我们的线名称 h
并在将新折线放在图形上之前将其删除。 HTH