Matlab – How to clear the last line in the command window


I'm curious about the progress of the running program and I print some information about the current iteration such as:

for i = 1:N
    msg = sprintf('Processed %d/%d', i, N);

I don't want to print the progress on separate lines, instead, I want the last line to replace the previous one. I don't want to use clc which clears all the content.

I know that '\b' can clear the last character (like backspace) and I can create a function with a for loop which clears the items till the previous new line before the last. But is there a better way to do that? If not, how can I check whether the last character on the command line is a new line or not?

Best Solution

I've looked at the problem, a while ago. And I've noticed that the character \r (used to erase the last line) works with matlab in command-line (-nodesktop) but not with the graphic mode...

The best solution I found is to do something like that:

for ...
Related Question