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

10 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by