MATLAB: Help! My first for-loop plot

for loopplotting

Hello.
I'm trying to make a simple for-loop plot, but I can't get the line to show! Where did it go? I even specified it as dashed blue line.
x=[0:0.1:3*pi];
y=sin(x);
for i=0:0.5:3*pi
if sin(i) > 0
plot(i,1,'--bx')
elseif sin(i) < 0
plot(i,-1,'--bx')
else
end
end
plot(x,y);

Best Answer

"I can't get the line to show! Where did it go?"
It didn't "go" anywhere, you never actually plotted it--in
if sin(i) > 0
plot(i,1,'--bx')
you're only plotting a single point each call and there's also not a hold statement so each new call to plot draws as if was first and only point on the axes.
Try
plot(x,sign(y),'--b')
hold all
plot(x,y)
ADDENDUM
If you really are trying to do animation here is reason for loop rather than just unfamiliarity with Matlab and vector operations, look at
doc addpoints % for the animatedline object