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);

 채택된 답변

dpb
dpb 2017년 2월 13일
편집: dpb 2017년 2월 13일

0 개 추천

"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

댓글 수: 3

Magnarok
Magnarok 2017년 2월 13일
편집: Magnarok 2017년 2월 13일
Like this? Still no line, and I tried change it to plot(x, sin(y), '-b'); as you said, but it plotted wrong poits for me.
x=[0:0.1:3*pi];
y=sin(x);
hold all
for i=0:0.1:3*pi
if sin(i) > 0
plot(i,1,'-bo');
elseif sin(i) < 0
plot(i,-1,'-bo');
else
end
end
plot(x,y);
dpb
dpb 2017년 2월 13일
No "not like that"... :) That's the same as you see you don't get a line but a series of points. You don't need a loop at all; the three lines I showed are the entire code required.
"change it to plot(x, sin(y), '-b') as you said,..."
That is NOT what I wrote at all...read more carefully!
EWWW!!! My bad I should TYPE and PROOFREAD more carefully!!! :(
Fixed in Answer.
Magnarok
Magnarok 2017년 2월 13일
편집: Magnarok 2017년 2월 13일
Thank you! Just started out MATLAB you see... :)
Got a hint elsewhere to use for-loop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2017년 2월 13일

편집:

2017년 2월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by