Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

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);
    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 '' 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've looked at the problem, a while ago. And I've noticed that the character (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('',1,n));
  fprintf(msg);
  n=numel(msg);
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...