Plotting a for loop

3 ビュー (過去 30 日間)
Salvie Morales
Salvie Morales 2019 年 9 月 16 日
コメント済み: Steven Lord 2019 年 9 月 16 日
Hi! I'm super new to MatLab so I am probably just making a dumb mistake but each time I try to plot my for loop I get a graph but no data is represented on it. Can someone set me straight? My entry looks like:
for i = 0:100;
C = i;
F = (C*1.8)+32;
plot (C,F)
xlabel('Celsius')
ylabel('Fahrenheit')
end
C
F

回答 (1 件)

Matt J
Matt J 2019 年 9 月 16 日
編集済み: Matt J 2019 年 9 月 16 日
C=nan(1,101); F=C; %preallocate
for i = 0:100;
C(i+1) = i;
F(i+1) = (i*1.8)+32;
end
plot (C,F)
xlabel('Celsius')
ylabel('Fahrenheit')
  2 件のコメント
Matt J
Matt J 2019 年 9 月 16 日
Or, with no looping,
C=0:100;
F=C*1.8+32;
plot(C,F)
Steven Lord
Steven Lord 2019 年 9 月 16 日
Matt J has given you two solutions. If you are required to plot inside the loop, you can create an animatedline before the loop and calling addpoints on the animatedline inside the loop. See the animatedline documentation page for examples.
As for why your original approach wasn't working, it was plotting a line each iteration through the loop. Each of those lines consisted of exactly one point; they weren't automatically connected to the previous lines created by previous loop iterations.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by