Use fprintf() or disp() with a newline character to print on separate lines

MATLAB prints text to the command window using fprintf() or disp(). To move text to a new line, add \n (the newline character) inside your string. The \n tells MATLAB to stop printing on the current line and start a fresh one below it.

The simplest approach is disp(), which automatically adds a newline at the end. If you need more control over spacing or formatting, use fprintf() and place \n wherever you want a line break. Both functions work in scripts and in the command window.

Key Takeaways

  • Use disp('text') to print a line and automatically move to the next line.
  • Use fprintf('text\n') to print text and add a newline where you place \n.
  • The \n character works inside double quotes but not single quotes in fprintf().
  • Multiple newlines in one string create blank lines: fprintf('line1\n\nline3') skips a line between them.
  • fprintf() lets you format numbers and variables using %d, %f, and %s placeholders.

Using disp() for straightforward line breaks

disp() is the fastest way to print text when you want each output on its own line. Every time you call disp(), MATLAB prints your text and moves the cursor to the next line automatically. You do not need to add anything special to the string itself.

Type this in the command window or in a script:

disp('First line') disp('Second line') disp('Third line')

MATLAB prints:

First line Second line Third line

This works for text, numbers, and variables. If you want to print a number, convert it to text first using num2str(): disp(num2str(42)). The disp() function is ideal when you are building a script that needs clean, readable output without worrying about formatting details.

Using fprintf() with \n for precise control

fprintf() gives you more control because you decide exactly where the newline goes. Inside double quotes, type \n at the point where you want to break to a new line. Unlike disp(), fprintf() does not automatically add a newline unless you include \n in your string.

Type this example:

fprintf('First line\n') fprintf('Second line\n') fprintf('Third line\n')

MATLAB prints the same output as the disp() example above. The difference is that if you forget the \n, the next output prints on the same line. This can be useful when you want to build a single line from multiple fprintf() calls, but it also means you must be intentional about where breaks occur.

You can also put multiple newlines in one fprintf() call:

fprintf('Line 1\nLine 2\nLine 3\n')

Printing variables and numbers with newlines

When you want to print a variable or number along with text, fprintf() is usually better because it lets you format the output. Use %d for whole numbers, %f for decimals, and %s for text. Place these placeholders where you want the variable to appear, then list the variables after the string.

Example with a variable:

age = 25; fprintf('My age is %d\n', age)

MATLAB prints: My age is 25

You can print multiple variables in one line:

name = 'Alice'; score = 87.5; fprintf('%s scored %f on the test\n', name, score)

MATLAB prints: Alice scored 87.500000 on the test

To control decimal places, use %.1f (one decimal) or %.2f (two decimals):

fprintf('%s scored %.1f on the test\n', name, score)

MATLAB prints: Alice scored 87.5 on the test

Common mistakes with newlines

The most common error is using single quotes instead of double quotes in fprintf(). Single quotes create a character array, and \n does not work as a newline inside them — it prints as two literal characters (a backslash and the letter n) instead of creating a line break.

Wrong: fprintf('Line 1\nLine 2') — this prints the backslash and n as visible characters in your output.

Right: fprintf("Line 1\nLine 2") — this creates an actual newline.

Another mistake is forgetting the newline at the end of your string. If you print multiple things without \n, they stack on the same line, making output hard to read. Always end with \n if you want the next output to start fresh. This is especially important in loops where you print many lines in sequence.

Printing blank lines and spacing

To add blank lines between outputs, use multiple \n characters in a row. Each \n moves down one line, so two in a row create one blank line between text blocks.

fprintf('Section 1\n\n') fprintf('Section 2\n')

MATLAB prints Section 1, then a blank line, then Section 2. This is useful for making long outputs easier to read and for separating different parts of your results visually.

You can also use fprintf('\n') by itself to print just a blank line:

fprintf('Line 1\n') fprintf('\n') fprintf('Line 3\n')

This creates a gap between Line 1 and Line 3 in the output. When you are printing results from calculations or loops, strategic blank lines make the command window output much easier to scan and understand.

Frequently Asked Questions

What is the difference between disp() and fprintf()?

disp() automatically adds a newline at the end, so each call prints on its own line. fprintf() only adds a newline where you put \n, giving you more control. Use disp() for quick output and fprintf() when you need to format numbers or print multiple things on one line.

Why does my newline not work in fprintf()?

You are probably using single quotes instead of double quotes. In MATLAB, \n only works as a newline inside double quotes. Single quotes treat it as two separate characters. Change fprintf('text\n') to fprintf("text\n").

Can I print a newline without printing anything else?

Yes. Type fprintf('\n') or disp(' ') to create a blank line. The first prints just a newline, and the second prints a space and then moves to the next line, creating visible spacing in your output.

How do I print a backslash without it being read as a newline?

Use two backslashes: fprintf('Path: C:\\Users\\Name\n'). The first backslash escapes the second, so MATLAB prints a single backslash instead of treating it as the start of a command like \n.

Can I use newlines inside a loop to print multiple results?

Yes, and this is common. Inside a for loop, use fprintf() with \n to print each result on its own line. For example, for i = 1:5, fprintf('Number: %d\n', i), end prints each number on a separate line instead of stacking them together.