Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the last line in the command window

Tags:

matlab

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?

like image 289
petrichor Avatar asked Jan 11 '12 19:01

petrichor


People also ask

How do you clear a command line?

Clear Command Prompt with CLS CommandEnter multiple standard commands into the blank cmd screen. Next type CLS and press Enter button. This will clear all the previously entered commands from the CMD screen in Windows.

How do I clear the command line in Linux?

Clear Terminal via Ctrl+L / Ctrl+Shift+K Shortcut Keyboard shortcuts also work for clearing the terminal, depending on the terminal emulator. An alternative in some terminal emulators is Ctrl + Shift + K . The command provides the same output as Ctrl + L .

How do you clear the last command in Matlab?

Use the clc command. and select the commands and figures you want to remove. to delete the selected items. Under History Settings, tap Clear All History.


3 Answers

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
like image 80
Oli Avatar answered Oct 22 '22 04:10

Oli


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
like image 19
gnovice Avatar answered Oct 22 '22 02:10

gnovice


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

like image 3
aspirin Avatar answered Oct 22 '22 04:10

aspirin