Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying information from MATLAB without a line feed

Is there any way to output/display information from a MATLAB program without an ending line feed?

My MATLAB program outputs a number a bit now and then. Between outputting the number the program does a lot of other stuff. This is a construct mainly to indicate some kind of progress and it would be nice not to have a line feed each time, just to make it more readable for the user. This is approximately what I'm looking for:

Current random seed:
4 7 1 1 

The next output from the program would be on the same row if it is still doing the same thing as before.

I've read the doc on disp, sprintf, and format but haven't found what I'm looking for. This doesn't mean it isn't there. ;)

like image 586
AnnaR Avatar asked Jun 18 '09 13:06

AnnaR


People also ask

How do I display output in Matlab?

disp( X ) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “ X = ” before the value. If a variable contains an empty array, disp returns without displaying anything.

How do you do a line break in Matlab?

c = newline creates a newline character. newline is equivalent to char(10) or sprintf('\n') . Use newline to concatenate a newline character onto a character vector or a string, or to split text on newline characters.

How do I print a blank line in Matlab?

fprintf('\n') underneath the line you're trying to make a space for.


1 Answers

The fprintf function does not add a line feed unless you explicitly tell it to. Omit the fid argument to have it print to the Command Window.

fprintf('Doing stuff... ');
for i = 1:5
    fprintf('%d ', i);
    % do some work on that pass...
end
fprintf(' done.\n'); % That \n explicitly adds the linefeed

Using sprintf won't quite work: it creates a string without a line feed, but then if you use disp() or omit the semicolon, disp's own display logic will add a line feed.

like image 196
Andrew Janke Avatar answered Sep 19 '22 17:09

Andrew Janke