简体   繁体   English

如何在 MATLAB 中更新冲浪 plot?

[英]How can I update a surf plot in MATLAB?

I try to simulate a robot motion by using a line, 'o' , as a robot.我尝试通过使用一条线'o'作为机器人来模拟机器人的运动。 I update its motion using set .我使用set更新它的动作。 But I have a problem to update the new position and angle of the robot's view angle.但是我更新新的position和机器人视角的角度有问题。 I use surf as a robot view angle, because I want to show color gradients.我使用surf作为机器人视角,因为我想显示颜色渐变。 Now I have to delete the surf plot, and replot it again every time.现在我必须删除surf plot,每次都重新绘制一遍。

How can I update the figure, rather than completely redrawing it every time?如何更新图形,而不是每次都完全重绘它? 在此处输入图像描述

This is my code:这是我的代码:

    classdef Robot6<handle
    properties
          velocity = 0;                         
          position = [0 0];
          angle=0;

    end
    methods
        function Runn(obj)
             set( gcf,'WindowKeyPressFcn',  @keyboard_down,'CloseRequestFcn', @close_window, 'WindowKeyReleaseFcn', @keyboard_up)
             set( gca,'color', 'white','xlim', [-100, 100],'ylim', [-100, 100])
             robot=line(obj.position(1), obj.position(2),'marker','o');
             hold(gca,'on')        
              program_on = 1;
     
              while program_on         
                userdataStore=[obj.velocity obj.angle obj.position]
                %pause(0.001);
                
                vel = [userdataStore(1)*cosd(userdataStore(2)),userdataStore(1)*sind(userdataStore(2))];
                pos = userdataStore(3:4);
                new_position = pos + vel;
                if (abs(new_position(1)) > 100 || abs(new_position(2)) > 100)
                    new_position = pos;
                end
                obj.position = new_position;
                
                set(robot, 'XData', new_position(1), 'YData', new_position(2));
                %set(orient,'XData', new_position(1)+2*cos(userdataStore(2)), 'YData', new_position(2)+2*sin(userdataStore(2)));   
                %delete(orient)
                x=[new_position(1) new_position(1)+10*cosd(userdataStore(2))+(10*tand(35)*cosd(90-userdataStore(2))) new_position(1)+10*cosd(userdataStore(2))-(10*tand(35)*cosd(90-userdataStore(2)))];
                y=[new_position(2) new_position(2)+10*sind(userdataStore(2))-10*tand(35)*sind(90-userdataStore(2)) new_position(2)+10*sind(userdataStore(2))+10*tand(35)*sind(90-userdataStore(2))];
                z=[100 0 0];
                xv = linspace(min(x), max(x), 1000);
                yv = linspace(min(y), max(y), 1000);
                [X,Y] = meshgrid(xv, yv);
                Z = griddata(x,y,z,X,Y);
                figure(1)
                orient=surf(X, Y, Z);
                grid off
                %set(gca, 'ZLim',[0 500])
                shading interp
                colormap(jet(80))
                view(2)
                pause(1)
                delete(orient)
                pause(0.00000000001)
        
              end
             delete(gcf);
              
             function close_window(~,~)
                program_on = 0;
             end
             
             function keyboard_down(~, event)
                switch event.Key
                    case 'downarrow'
                        obj.velocity = -5;
                    case 'uparrow'
                        obj.velocity = 5;    
                    case 'leftarrow'
                        obj.angle = obj.angle + 6;
                    case 'rightarrow'
                        obj.angle = obj.angle - 6;
                    otherwise
                        disp('unknown key');
                end
             end
             
            function keyboard_up(~,~)
                obj.velocity = 0;
            end
            
        end
        
    end
end

You can do that the same way you update your robot graphic object.您可以像更新robot图形 object 一样执行此操作。 Initialise an empty surf object just after you initialise the robot , then in the while loop you only update the surface with set .在你初始化robot之后初始化一个空的surf object,然后在 while 循环中你只用set更新表面。

So for your code, just under your lines:因此,对于您的代码,就在您的行下:

robot=line(obj.position(1), obj.position(2),'marker','o');
hold(gca,'on')

declare the empty surface object (it will exist but will not apear at this point because it is empty of data point):声明空表面 object (它将存在但不会出现,因为它是空的数据点):

orient = surf([],[],[]) ;

Then in the main while loop, replace the line:然后在主while循环中,替换以下行:

orient=surf(X, Y, Z);

with this one:有了这个:

set(orient,'XData',X,'YData',Y,'ZData',Z)

That answers your main question on how to update a surf plot instead of redrawing it every time.这回答了您关于如何更新surf plot 而不是每次都重绘它的主要问题。 Note that now you are not recreating a plot at every loop, a lot of the axes and figure properties won't be recalculated at each update, so you can also take all these lines:请注意,现在您不会在每个循环中重新创建 plot,很多axesfigure属性不会在每次更新时重新计算,因此您也可以采用所有这些行:

grid off
%set(gca, 'ZLim',[0 500])
shading interp
colormap(jet(80))
view(2)

out of the main loop and place them right after the surface creation.离开主循环并将它们放置在表面创建之后。 These properties will stay like that and don't need to be set again at each loop iteration.这些属性将保持不变,不需要在每次循环迭代时再次设置。

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

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