为数字的某些部分设置保持,而不是其他部分

时间:2014-02-24 20:46:57

标签: matlab

下面是我的matlab代码,它在原点周围旋转时绘制一条黑线。在大多数情况下代码都有效,但因为我使用'hold on',一旦我绘制了一条线,它就会保留在整个程序中。

我想要的是更像是钟表上的秒针(仅逆时针移动)。对于i的特定值,打印黑线,并且在i递增之后,前一行消失。但是,我希望红色和蓝色线以及绘制的文本保持在从迭代到迭代的图上。我尝试使用set()命令,无法让它工作,任何想法?

scalar = 3;

a = [3/scalar 2/scalar];
b = [-4/scalar .5/scalar];

starts = zeros(2,2);
ends = [a;b];

hold on
quiver(0,0,a(1),a(2),'color',[0 0 1],'linewidth',3);
quiver(0,0,b(1),b(2),'color',[1 0 0],'linewidth',3);
axisSize = 15/scalar;
axis([-axisSize axisSize -axisSize axisSize])

mintheta = [500, 500];
%waitforbuttonpress
for i=1:360
    theta = [cos(2*pi()*i/360) sin(2*pi()*i/360)];
    minDot = min(a*theta',-1*(b*theta'));
    theta = theta/minDot;

    hold on
    if (minDot > 0)
        if (norm(theta) < norm(mintheta))
            mintheta = theta;
        end

        text(-4.5, 4, 'Drawing all Theta vectors,');
        text(-4.5, 3.7, 'according to constraint that');
        text(-4.5, 3.4, 'all dotProducts >= 1');
        text(-4.5, 2.5, 'Yellow indicates a non-separating plane,');
        text(-4.5, 2.2, 'SVM looks for shortest possible');
        text(-4.5, 1.9, 'Theta (will be shown in green).');
        quiver(0,0,theta(1),theta(2),'color',[0 0 0],'linewidth',5);
        quiver(0,0,3/scalar,2/scalar,'color',[0 0 1],'linewidth',3);
        quiver(0,0,-4/scalar,.5/scalar,'color',[1 0 0],'linewidth',3);
        axis([-axisSize axisSize -axisSize axisSize])
    else
        quiver(0,0,cos(2*pi()*i/360),sin(2*pi()*i/360),'color',[1 1 0],'linewidth',5);
    end

    pause(.05);
end
quiver(0,0,mintheta(1),mintheta(2),'color',[0 1 0],'linewidth',3);

1 个答案:

答案 0 :(得分:2)

这应该让你开始:

% set up axis
axisSize = 5;
axis([-axisSize axisSize -axisSize axisSize])
cla

hold on

% draw quiver at initial position
q = quiver(0,0,cos(2*pi()*0/360),sin(2*pi()*0/360),'color',[0 0 0],'linewidth',5);

for i = 1:360

   % modify data inside quiver plot
   set(q,...
       'xdata',0,...
       'ydata',0,...
       'udata',cos(2*pi()*i/360),...
       'vdata',sin(2*pi()*i/360)) 

   pause(.05);
end

另外,我会把文字放在循环之外。它只需要创建一次,并且由于某种原因,MATLAB不能很好地渲染相同文本的堆栈。