0
votes

Below is my matlab code that plots a black line as it rotates around the origin. For the most part the code works, but because I use 'hold on', once I plot a line, it stays there for the entirety of the program.

What I'd like is something more like a second hand on a clock (only moving counter clockwise). For a particular value of i, the black line prints, and after i increments, that previous line is gone. However, I would like the red and blue lines and the plotted text to stay on the plot from iteration to iteration. I tried using the set() command and couldn't get it working, any ideas?

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

1 Answers

2
votes

This should get you started:

% 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

Also, I would put the text outside the loop. It only needs to be created once, and MATLAB doesn't render stacks of identical text very nicely for some reason.