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

11 views (last 30 days)
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:)

Accepted Answer

Walter Roberson
Walter Roberson on 16 Mar 2014
Before you start the loop,
fprintf('Iteration =');
In the loop,
fprintf(' %d', iteration);
after the loop:
fprintf('\n');
  6 Comments
Walter Roberson
Walter Roberson on 16 Mar 2014
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!