HOWTO · MATLAB

How to Escape Character Sequences on a String in MATLAB

This tutorial demonstrates how to escape character sequences in strings in MATLAB. Learn the methods to use escape characters effectively, including examples for including quotes, new lines, and backslashes. Enhance your MATLAB programming skills with these string manipulation techniques.

On this page

To put quotes or a backslash in MATLAB text, first distinguish the text literal from formatting. A backslash is an ordinary character in a character vector written with single quotes; sprintf interprets backslash sequences such as \n and \t when it builds formatted text. This distinction helps you choose syntax that produces the characters or line layout you intend.

How MATLAB Handles Escape Sequences

MATLAB uses different rules for character vectors and formatting functions. In a character vector such as 'C:\Temp', the backslash is literal; it does not escape the next character. A backslash sequence such as \n has a special meaning when passed to sprintf, which converts it to a line feed. sprintf also processes format placeholders such as %s and %d.

This means that typing \n inside an ordinary single-quoted character vector does not, by itself, start a new line. Use a function that interprets the sequence when you need formatted control characters. Keep that distinction in mind when adapting examples from languages where backslashes are interpreted directly in string literals.

Use sprintf to Create Escape Sequences

Use sprintf to combine text with formatting sequences. Its first input is a format pattern: \n starts a new line, \t inserts a horizontal tab, and placeholders such as %s are replaced by later inputs. The result is text that you can display or store.

Run this example:

apostrophe = 'It''s ready.';
quote = 'She said, "Hello, MATLAB!"';
path = 'C:\Temp\report.txt';
message = sprintf('%s\n%s\n%s\n\t%s', apostrophe, quote, path, 'Done');
fprintf('%s\n', message);
It's ready.
She said, "Hello, MATLAB!"
C:\Temp\report.txt
	Done

This output is from Octave; the syntax shown follows MATLAB character-vector and sprintf rules, but no MATLAB runtime result is supplied. The doubled apostrophe represents one apostrophe in a character vector. The double quotes and single backslashes in path are literal characters, while the backslash sequences in the format pattern are interpreted by sprintf.

Include Quotes and Backslashes

Inside a single-quoted character vector, write an apostrophe twice: 'It''s ready.' produces It's ready. A double quote has no special role in this character-vector syntax, so write it directly, without a backslash: 'She said, "Hello!"' produces She said, "Hello!".

A backslash in a character vector is also literal. For example, 'C:\Temp\report.txt' contains the backslashes as typed. If you need a backslash generated by sprintf, write \\ in its format pattern because sprintf interprets each pair as one literal backslash. Do not apply the formatting rule to ordinary character-vector literals.

Add Newlines and Tabs

For line breaks and indentation, put \n and \t in the sprintf format pattern. In the example above, \n separates each item onto a new line and \t indents Done. The visible tab width depends on the program that displays the text, so its alignment can vary; the tab character itself is what sprintf inserts.

If you already have text and want to display it with a line break, add the newline in the display call, for example fprintf('%s\n', message). disp also displays text followed by a line break, but it does not interpret backslash sequences inside the text you pass to it.

Troubleshoot Unexpected String Output

If \n appears as two visible characters, check whether it was placed in a character vector rather than an sprintf format pattern. If a quote ends the literal unexpectedly, double an apostrophe inside a single-quoted character vector. A double quote inside that form is written directly, without a preceding backslash.

If a path loses or changes its backslashes, check which operation created it: a normal character vector preserves them, while a formatting function processes backslash sequences. When building formatted text, use \\ in the sprintf pattern for one literal backslash. Finally, inspect the exact format string and its matching inputs: %s expects text, and each placeholder should have a corresponding value. The feature image’s quote row is ambiguous: write a double quote directly in a single-quoted character vector, and double an apostrophe. These checks separate literal-text rules from formatting behavior and make unexpected output easier to locate.