In MATLAB 7.8 (R2009a) loop counter values can be displayed in the command window without carriage returns by using the FPRINTF command.
The following code displays counter values with a space between each successive value instead of a carriage return.
for i=1:10
fprintf('%d ', i);
end
The following modified version of the code above uses the backspace escape command '\b' to delete the previous counter value. This increments the counter in place rather than in a line of increasing length.
fprintf('\nCounter: ')
for i=1:100
if i>1
for j=0:log10(i-1)
fprintf('\b');
end
end
fprintf('%d', i);
pause(.05);
end
fprintf('\n')
An implementation of a redefinition of FPRINTF for carriage return without linefeed has been published on the following MATLAB Central file:
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.
Best Answer