OneZero
OneZero

Reputation: 11904

How to print carriage return in matlab?

Simply using \r doesn't work as it is handled as a new line:

>> fprintf('a\rb');
a
b>> 

So what is the correct way of printing a carriage return so I can update information without gathering a whole of junk on the screen.

Upvotes: 3

Views: 10506

Answers (2)

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

Here is a solution, obviously not as clean as a \r:

nchar = fprintf('abcd');
fprintf(repmat('\b', 1, nchar));
fprintf('ABCD');

For instance in a loop:

for k=1:10,
    nchar = fprintf('Processing %d of %d', k, 10);
    pause(0.10);
    fprintf(repmat('\b', 1, nchar));
end

Upvotes: 4

Shaun314
Shaun314

Reputation: 3461

\n or char(10) should do the trick!

If you are trying to do this for guis, I recommend the following:

['FIRST LINE' char(10) 'SECOND LINE'] etc.

Or I think sprintf will be able to do the "\n" for you, but I am not positive about that.

If in GUIDE, on the string section of the property inspector, you can actually double click for an edit box which will do new lines, it is kind of confusing at first.

Upvotes: 0

Related Questions