How do I show a changing variable value in command window?

10 ビュー (過去 30 日間)
Luis
Luis 2014 年 3 月 16 日
コメント済み: Luis 2014 年 3 月 16 日
I am doing kind of Genetic Algorithm problem. It takes dozens of iterations before coming to a satisfactory solution. I want to show how the number of iteration grows IN ONE ROW in the command window.
I only know how to do it in rows one after another, here's the code:
if %Stopping Criterion%
break;
else
iteration = iteration +1 ;
fprintf('Iteration = %d \n',iteration);
end
As you know , in the command window, this will be:
Iteration = 2
Iteration = 3
Iteration = 4
Iteration = 5
Iteration = 6
Iteration = 7
.
.
.
My concern is, I want to display the result ONLY IN ONE ROW, that is, "Iteration =" does not show again, ONLY the growing value of the iteration changes.
How could I make it?
Thanks in advance:)

採用された回答

Walter Roberson
Walter Roberson 2014 年 3 月 16 日
Before you start the loop,
fprintf('Iteration =');
In the loop,
fprintf(' %d', iteration);
after the loop:
fprintf('\n');
  6 件のコメント
Walter Roberson
Walter Roberson 2014 年 3 月 16 日
The 0000 is not important other than it being 4 characters long so that you get positioned at the right position. Each \b backspaces one position so \b\b\b\b moves to the beginning of those 4 positions. The %4d says to use 4 characters (more if needed) to output the number... leaving you at the same position on the screen as you were after the 0000 . Thus each line after that backs up over the previous number and puts in a new 4 character number.
If your iteration count might exceed 9999 then you would need to increase the number of characters initially printed and you would need to add more \b
Luis
Luis 2014 年 3 月 16 日
Perfect teacher!
Thanks again!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by