Why is my function outputting a blank graph:

1 回表示 (過去 30 日間)
Fardin Khan
Fardin Khan 2019 年 1 月 20 日
回答済み: Walter Roberson 2019 年 1 月 20 日
function add(a, b, n)
figure, hold on
for i=1:n
a = [a+2];
b = [b*2];
fprintf("%8.3f", a), fprintf("%19.4f\n", b);
plot(a, b)
end
end

回答 (2 件)

per isakson
per isakson 2019 年 1 月 20 日
編集済み: per isakson 2019 年 1 月 20 日
Replace
plot(a,b)
by
plot(a,b,'d')
and look up plot in the documentation

Walter Roberson
Walter Roberson 2019 年 1 月 20 日
plot() creates line() objects. Each line object is drawn independently of the others, and only connects the points listed in the one line object. Also the default is not to put in any markers. Your a and b are scalars, so when you plot(a,b) they have no point to connect to so no line is drawn, and since the default is no marker, the individual points are not drawn. per's solution is to add a marker, so at least one point at a time would be drawn.
In order to have the points connected, you can:
  • remember the previous point and draw back to it, so the graph would turn out to be made of a number of small lines; or
  • store all of the points until the end of the loop and then plot all of the stored points at one time, so that a single line is drawn connecting all of them; or
  • use animatedline() to create a basic line, and then each cycle of the loop, addpoints() to extend the line.

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by