How to Print a New Line in MATLAB: Methods, Contexts, and Key Differences

Printing a new line in MATLAB sounds straightforward — and in many cases it is. But depending on where your output is going (the Command Window, a file, a GUI element, or a string variable), the method you use matters. Getting it wrong produces cluttered output, missing line breaks, or characters that appear literally instead of as formatting.

Here's a clear breakdown of how new lines work in MATLAB and what shapes the right approach for any given situation.

The Core Concept: Escape Characters in MATLAB 🖨️

MATLAB uses escape sequences inside strings to represent special characters like new lines, tabs, and carriage returns. The new line escape sequence is .

However, there's a catch: only works as a new line inside specific string types and functions. In a standard single-quoted MATLAB string like 'Hello World', the is treated as two literal characters — a backslash and the letter n — not as a line break.

To make behave as an actual new line, you have two main paths:

  • Use it inside fprintf or sprintf, which interpret escape sequences
  • Use a double-quoted string (introduced in MATLAB R2017a), where is automatically interpreted

This distinction trips up a lot of users, especially those coming from Python or C where string handling works differently.

Method 1: Using fprintf to Print a New Line

fprintf is the most common and reliable way to print formatted text with new lines in MATLAB's Command Window or to a file.

fprintf('First line Second line ') 

This outputs:

First line Second line 

fprintf processes escape sequences by default, so becomes a real line break. You can stack multiple sequences for blank lines between output blocks.

Key point: If you're printing to a file, fprintf takes a file identifier as the first argument. When no file ID is provided, it defaults to standard output (the Command Window).

Method 2: Using sprintf to Build a String with New Lines

sprintf works the same way as fprintf for escape sequences, but instead of printing directly, it returns the formatted string as a variable. This is useful when you need to store or pass a string that includes line breaks.

msg = sprintf('Line one Line two '); disp(msg) 

The variable msg now contains an actual newline character embedded in it, which disp will render correctly.

Method 3: Double-Quoted Strings (R2017a and Later)

Since MATLAB R2017a, double-quoted strings ("...") are a distinct string type that interprets escape sequences natively — no need for fprintf or sprintf.

str = "Hello World"; disp(str) 

This prints:

Hello World 

This method is clean and modern, but it only works in MATLAB R2017a or newer. If you're working in an older environment or maintaining legacy code, single-quoted character arrays are still common and fprintf/sprintf remain the standard tools.

Method 4: Using char(10) or newline

Another approach — particularly useful in code that needs to be version-agnostic or when building strings programmatically — is to use the ASCII code for a line feed directly.

disp(['First line' char(10) 'Second line']) 

MATLAB R2019b introduced the newline function as a cleaner alias:

disp(['First line' newline 'Second line']) 

Both produce the same result. char(10) is the line feed character (ASCII 10), which is the standard new line on Unix/Linux/macOS systems. On Windows, a full newline is technically (carriage return + line feed), but MATLAB typically handles line endings correctly across platforms without requiring char(13) manually.

Comparing the Methods at a Glance 📊

MethodInterprets ?Output TargetVersion Required
fprintf with ✅ YesCommand Window / FileAny
sprintf with ✅ YesString variableAny
Double-quoted string "..."✅ YesString variable / displayR2017a+
char(10)N/A (direct)String concatenationAny
newline functionN/A (direct)String concatenationR2019b+

Variables That Affect Which Method Works Best

MATLAB version is the first variable. Older codebases and shared scripts running on legacy versions can't rely on double-quoted strings or the newline function. fprintf and char(10) remain the safest cross-version options.

Output destination matters significantly. Printing to the Command Window, writing to a text file, building a string for a UI label, or passing a message to a logging function each have different requirements. fprintf with a file ID handles file output cleanly. sprintf is better when the string needs to travel through the code before being displayed.

String type in use is another variable. MATLAB has two string representations: character arrays (single-quoted) and string objects (double-quoted). They behave differently in functions, comparisons, and concatenation. Mixing them without care can produce type errors or unexpected output.

Intended formatting precision also plays a role. If you're producing structured output — reports, logs, formatted tables — fprintf with explicit placement gives the most control. For quick diagnostic output, disp with newline concatenation is often faster to write.

A Note on in disp vs. fprintf 🔍

A common source of confusion: disp('Hello World') does not produce a line break. It prints literally:

Hello World 

disp does not process escape sequences. It only displays the value of whatever you pass it. To get a new line with disp, you either concatenate newline/char(10) into the string, or use a double-quoted string that has already interpreted the escape sequence.

Whether fprintf or disp-with-concatenation is more readable in your code depends on your team's conventions, the MATLAB version in play, and whether you're formatting output with variables (where fprintf's format string syntax becomes genuinely valuable) or printing fixed messages.