The simplest way to print a new line in MATLAB is the newline character, written as newline or the escape sequence \n

When you want text to appear on separate lines in MATLAB output, you have two main approaches. The newline function creates a line break directly, while the backslash-n escape sequence \n does the same thing inside a string. Both work in the Command Window, in scripts, and when writing output to files.

Which one you choose depends on what you are printing and how your code reads. The newline function is explicit and clear. The \n escape sequence is shorter and works inside any string without extra syntax. Neither is faster or more correct — pick whichever makes your code easier to read.

Key Takeaways

  • Use newline or \n inside fprintf, sprintf, or disp to print text on separate lines.
  • The newline function returns a line break character you can concatenate with strings using square brackets or the + operator.
  • The \n escape sequence works only inside double-quoted strings, not in single-quoted character arrays.
  • For multiple lines of output, fprintf with \n is usually faster and cleaner than calling disp multiple times.

Using newline with disp and concatenation

The newline function returns a single line break character. You can concatenate it with text using square brackets or the + operator (in MATLAB R2016b and later). This approach works well when you are building a string piece by piece.

For example, this code prints two lines:

disp("Hello" + newline + "World")

Or using square brackets:

disp(["Hello" newline "World"])

Both produce the same output: Hello on the first line and World on the second. The square bracket method works with older MATLAB versions and with character arrays, while the + operator only works with strings (the newer text type introduced in R2016b).

Using \n inside fprintf and sprintf

The escape sequence \n is the standard way to add line breaks when you are using fprintf or sprintf. Place \n wherever you want a new line to start. This method is compact and works in any double-quoted string.

This code prints three lines with values:

x = 5; y = 10; fprintf("x is %d\ny is %d\n", x, y)

The output will be:

x is 5 y is 10

Notice that \n appears in the format string wherever you want a line break. The %d placeholders are replaced with the values of x and y. The final \n moves the cursor to a new line after the last value, which is good practice so the next command prompt appears on its own line.

Why \n does not work in single-quoted strings

MATLAB treats single-quoted strings as character arrays, not as text strings. In character arrays, \n is stored as two separate characters — a backslash and the letter n — rather than as a line break. This is a common source of confusion.

This code will not produce a line break:

disp('Hello\nWorld') % Wrong: prints "Hello\nWorld" literally

To fix it, use double quotes instead:

disp("Hello\nWorld") % Correct: prints Hello on one line, World on the next

If you must use single quotes, use the newline function or the numeric escape code char(10) instead. The number 10 is the ASCII code for a line feed character.

Printing multiple lines in a loop

When you need to print many lines — for instance, the results of a calculation for each row in a matrix — fprintf with \n is usually the cleanest approach. It avoids calling disp repeatedly, which can be slower.

This example prints a table of values:

for i = 1:5   fprintf("Row %d: %f\n", i, i*2.5) end

The loop runs five times, and each iteration prints one line with the row number and a calculated value. The \n at the end of the format string ensures each row appears on its own line. If you used disp inside the loop instead, the code would be longer and potentially slower on large loops.

Combining newline with sprintf for stored output

The sprintf function works like fprintf but stores the result in a variable instead of printing it immediately. You can use \n or newline the same way you would in fprintf.

This code builds a multi-line message and stores it:

name = "Alice"; age = 30; message = sprintf("Name: %s\nAge: %d", name, age)

The variable message now contains the text with a line break between the name and age. You can print it later with disp(message), write it to a file, or pass it to another function. This approach is useful when you need to build output before deciding what to do with it.

Frequently Asked Questions

Can I use \n in the Command Window directly?

Yes. Type disp("text\nmore text") or fprintf("text\nmore text\n") directly into the Command Window and press Enter. The line break will appear in the output. Single-quoted strings will not work — you must use double quotes.

What is the difference between \n and \r\n?

The sequence \n is a line feed (moves to the next line). The sequence \r\n is a carriage return followed by a line feed, which is the standard line ending on Windows. For most MATLAB output, \n alone is sufficient. Use \r\n only if you are writing to a file that will be read by Windows software and you want exact Windows line endings.

Does newline work the same way as \n?

They produce the same result in output, but they work differently in code. The newline function returns a character that you concatenate with other text. The \n escape sequence is written directly inside a string. Use newline when building strings with concatenation; use \n when writing format strings for fprintf or sprintf.

How do I print a literal backslash-n instead of a line break?

Use two backslashes: \\n. The first backslash escapes the second, so MATLAB prints a single backslash followed by the letter n. For example, fprintf("Use \\n for a newline\n") will print the text "Use \n for a newline" and then move to a new line.