Why are newline format specifiers not interpreted as one would expect in MATLAB when using the FPRINTF function?

1 visualizzazione (ultimi 30 giorni)
The FPRINTF function should yield the same results in the following two examples, but does not do so in MATLAB:
Case 1: When '%f' tag is not used
str= '1\n2\n3\n4\n';
fprintf(str);
The output is:
1
2
3
4
Case 2: When '%f' tag is used
fprintf('%s',str);
The output is:
1\n2\n3\n4\n

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 27 Giu 2009
This is expected behavior in MATLAB.
In the first case, 'str' variable is interpreted as a format string, and the special syntax '\n' is interpreted as a format specifier, i.e. a newline. In the second case, 'str' variable is an argument into the format, and is printed literally.

Più risposte (0)

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Prodotti


Release

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by