简体   繁体   English

如何在Matlab中实时绘制3D点?

[英]How do I plot 3D points in real time in Matlab?

I am confused on the best way to implement plotting 3D points live in Matlab. 我对在Matlab中实现绘制3D点的最佳方法感到困惑。 One idea I had is to keep updating an array of x,y,z values and then plot that, but I want it to plot live, as I'm using a LiDAR scanner for a project and want to show a 3D map being created. 我的一个想法是继续更新x,y,z值的数组,然后绘制它,但是我希望它能实时绘制,因为我正在为项目使用LiDAR扫描仪,并希望显示正在创建的3D地图。 This is the example I've been trying to work off of: 这是我一直在尝试的示例:

x = rand(10,1) ; y = rand(10,1) ;
h = scatter(x,y,'Or') ;
for i = 1:100
    x = rand(10,1) 
    y = rand(10,1) ;
    set(h,'XData',x,'YData',y) ;
    drawnow
    pause(0.1)
end

I have one problem with this, and it persists when I move to adding a Z value as well. 我对此有一个问题,当我继续添加Z值时,它仍然存在。 The plotting works, even in 3D, but then every iteration of the loop removes all of the previous points and then replaces them with the new points. 即使在3D模式下,绘图也能正常工作,但是循环的每次迭代都会删除所有先前的点,然后将其替换为新的点。 As I'm making a map, I need the points to be maintained with maximum efficiency. 在制作地图时,我需要以最高的效率维护这些点。

So, I'm wondering if there is a way to plot in real time without having to plot the entire thing over and over again, instead just "appending" the points onto the 3D plane. 因此,我想知道是否存在一种实时绘制的方法,而不必一遍又一遍地绘制整个事物,而只是将这些点“附加”到3D平面上。 If there isn't, I would want to know how to fix my code so that points will not be replaced. 如果没有,我想知道如何修复我的代码,以使这些点不会被替换。

Instead of set(h,'XData',x,'YData',y) ; 代替set(h,'XData',x,'YData',y) ; , hold on with new plot could enable you append something onto the plane, so the code following might work: hold on使用新的绘图,可以使您在平面上附加一些内容,因此以下代码可能有效:

x = rand(10,1) ; y = rand(10,1) ;
h = scatter(x,y,'Or') ;
hold on      % allow appending
for i = 1:100
    x = rand(10,1) 
    y = rand(10,1) ;
    h = scatter(x,y,'Or') ;   %draw new points
%     set(h,'XData',x,'YData',y) ;
    drawnow
    pause(0.1)
end

But keeping all datas, such as saving new statistics into a new column which is just behind the old ones, is highly recommended: 但是强烈建议保留所有数据,例如将新统计信息保存在旧数据之后的新列中:

[x,y] = deal(zeros(10,100));           %pre-allocate
figure(1); hold on
for ii = 1:100
    x(:,ii)=rand(10,1);                %save new x
    y(:,ii)=rand(10,1);                %save new y
    scatter(x(:,ii),y(:,ii),'Or') ;    %plot them
    drawnow
    pause(0.1)
end

Moreover, hold on has downside sometimes when we need further editing the figure by dealing with the handle, as every ten points would have unique handle. 此外, hold on有时下行的时候,我们需要将把手处理,如每十个点将具有独特的手柄还编辑的身影。 To keep them all in one, I think a minor change to your code might be a better solution if we don't have too much data to plot(It demands re-allowcate vectors in RAM, 'clean' the figure, and replot all the points, including those old one, again and again). 为了将它们保持一体,我认为如果我们没有太多数据要绘制,对代码进行较小的更改可能是一个更好的解决方案(它要求重新允许RAM中的向量,``清理''图形并重新绘制所有图形)点,包括那些旧的点,一次又一次)。

x = rand(10,1) ; y = rand(10,1) ;
h = scatter(x,y,'Or') ;
for i = 1:100
    x(end+1:end+10) = rand(10,1) ;     % append x
    y(end+1:end+10) = rand(10,1) ;     % append y
    set(h,'XData',x,'YData',y) ;       % draw new figure
    drawnow
    pause(0.1)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM