Hello,
i want to input a linebreak ( via '\n' ) into a string. I mean something like this:
a =sprintf('A%sA', '\n');
%this produces
a =
A\nA
%but i want it to produce
a =
A
A
How would I do that?
/edit: this is a minimal example just to show my point, of course. The case i want to work with has multiple inputs and a more complex formatSpec..

 Réponse acceptée

Jan
Jan le 6 Mai 2019
Modifié(e) : Jan le 6 Mai 2019

2 votes

a = sprintf('A\nA')
% Or
a = sprintf('A%cA', char(10)) % %s would work also
% Use |newline| instead of char(10) in modern Matlab versions
Your code is the correct method to insert tha characters '\n' directly:
a = sprintf('A%sA', '\n');
% Equivalent to:
a = sprintf('A\\nA');
This is useful, if a file name is printed with path, because this is not reliable:
sprintf(['File: ', filename, '\n'])
If filename contains a control character as \n, \t, \c or e.g. a %s, the output will be confusing. The same must be considered for warnings and errors:
error(['Failing: ', filename])
A clean way without danger of confusing output:
error('Failing: %s', filename)

5 commentaires

Also worth considering:
a = sprintf('A%cA',sprintf('\n'))
But something like that would not be possible:
inputBlock = 'the First Line\nthe Second Line\nAnd The Third for good measure';
sprintf('this is some large formatSpec\nAnd there comes my block:\n\n%s\n\nThis is after the block', inputBlock)
Stephen23
Stephen23 le 6 Mai 2019
Modifié(e) : Stephen23 le 6 Mai 2019
"But something like that would not be possible:"
Correct. But you can easily do this:
inputBlock = sprintf('the First Line\nthe Second Line\nAnd The Third for good measure');
...
With the exception of the apostrophe, in MATLAB all characters are interpreted literally in character vector definitions. You can use escaped characters in the format string of sprintf or fprintf or error (note that it is the function itself that interprets the escaped characters, the escaped characters are still literally present in the format string itself).
Jan
Jan le 6 Mai 2019
Modifié(e) : Jan le 6 Mai 2019
@Andreas: The rules are:
  • In the format specifier of sprintf and fprintf the \ means, that the following character is treated as control. Examples: \n \r \t \a \b \10
  • To include a \ as character in a format specifier, use \\
  • Anywhere else in a string or char vector, a \ is simply \
So to include a line break (which is CHAR(10)), either include \n in the format specifier, or a char(10) in the string or char vector. So these commands produce the same results:
a = char(10)
b = sprintf('\n')
isequal(a,b) % Yes!
a = ['Line1', char(10), 'Line2']
b = sprintf('Line1\nLine2')
c = sprintf('%s\n%s', 'Line1', 'Line2')
d = sprintf('%s%c%s', 'Line1', char(10), 'Line2')
isequal(a, b, c, d) % Yes
Clear now?
I want to increase the visiblity of one suggestion Jan made earlier in a comment in the original answer. if you're using release R2016b or later, I recommend using newline instead of char(10). IMO this makes the code author's intent very clear and avoids "magic numbers".
A = ['Line 1', newline, 'Line 2']
Note however that A is still a row vector even though it is displayed like it had two rows.
>> isrow(A)
ans =
logical
1

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Produits

Version

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by