petrichor
petrichor

Reputation: 6579

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);
    display(msg)
end

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?

Upvotes: 21

Views: 50035

Answers (5)

JDQ
JDQ

Reputation: 462

Another solution that overwrites the entire previous line relies on the \r formatting character,

ctrl=0;
while ctrl<5
    fprintf('\rctrl: %i',ctrl);
    ctrl=ctrl+1;
    pause(2); % To highlight overwrite 
end
fprintf('\n'); % Don't forget the newline

Upvotes: 0

aspirin
aspirin

Reputation: 347

I use 'dispstat' function just for this purpose. It can update the previous output which is a missing function of default 'disp'. Very simple to use. It can be downloaded from here: http://www.mathworks.com/matlabcentral/fileexchange/44673-overwritable-message-outputs-to-commandline-window

***Sample usage:

 dispstat('','init'); % One time only initialization
 dispstat(sprintf('Begining the process...'),'keepthis','timestamp');
 for i = 97:100
     dispstat(sprintf('Progress %d%%',i),'timestamp');
     %doing some heavy stuff here
 end
 dispstat('Finished.','keepprev');

***Output:

11:25:37 Begining the process...
11:25:37 Progress 100%
Finished.

All the best

Upvotes: 3

Oli
Oli

Reputation: 16035

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:

n=0;
for ...
  ...
  fprintf(repmat('\b',1,n));
  fprintf(msg);
  n=numel(msg);
end

Upvotes: 33

gnovice
gnovice

Reputation: 125854

Yair Altman has a very nice example on his blog of how you can use the backspace control-character (\b) to do what you want but in an easier way than you were considering. Modifying your code to resemble his example, you could do something like this:

reverseStr = '';
for i = 1:N
    ...
    ...
    msg = sprintf('Processed %d/%d', i, N);
    fprintf([reverseStr, msg]);
    reverseStr = repmat(sprintf('\b'), 1, length(msg));
end

Upvotes: 20

Azim J
Azim J

Reputation: 8290

Is this about what you are looking for

%# create title
fprintf('processed: %03d',0)
for i=1:10
    %# delete last three digit number and replace with new
    %# loop index
    fprintf('\b\b\b\b %03d',i);

    %# process here
    pause(.5)
end
%# clear line
fprintf('\n');

But if your code displays other results this won't work. and you might want to consider using a message box to update progress.

Upvotes: 2

Related Questions