tags:

views:

1434

answers:

2

I have a m-file that outputs some calculations basically this:

disp(['Value ', num2str(i)]);
disp(x)
disp(['Number of iterations ', num2str(iter)])
disp('----------')

However this ouputs stuff rather messy in the command view which is really irritating when debugging the code. I would like to add a couple of line breaks to the output in the command window. However I can't seem to find any information about this, as the Matlab documentation is pretty awful. I've tried stuff like disp('\n') and disp(' ') to no avail.

How do you do it? Can it be done?

+8  A: 

fprintf('\n') should do the trick, likewise disp(' '). In general, fprintf is more flexible than disp. The main advantage of disp is that it has some intelligence and knows how to print out complete objects.

Edric
Thanks! fprintf('\n') did the trick. Didn't know that fprintf could output text. Nice that you can also do fprintf('Juhi = %d',17). However disp('') still doesn't output a line-break might be a version issue or something.
Reed Richards
disp('') produces no output, but disp(' ') (with a space) is good enough for most practical purposes. If not, use fprintf as suggested or disp(s) where s is a string containing what you need.
groovingandi
oops, yes, disp(' ') is correct
Edric
+1  A: 

You can also disp a the line break character '\n' with its decimal value: 10.

disp(char(10))

or

disp(['line 1' char(10) 'line 2'])
Mike Katz
The first one doesn't work. You need to write `disp(char(10))`, otherwise it just displays the number 10.
gnovice
@gnovice...so it does, thanks. I had gotten used to other text methods that don't have a override for doubles. Good catch.
Mike Katz
I think the second line of code would have worked as it was before. Concatenating chars and doubles should convert the doubles to their ASCII equivalents.
gnovice
Minor nitpick: modern Matlab chars are Unicode (stored as 16-bit), so strictly speaking, numerics are converted to Unicode code points, not ASCII. "Extended ASCII" (128-255) characters may differ, and higher numbers are valid chars. E.g. char(338) will display a ligature "OE".
Andrew Janke
@Andrew: You're correct. I need to break myself of the habit of saying "ASCII", but it's hard when the documentation still mentions it in some places. ;)
gnovice

related questions