Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jun. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Produkte


Version

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by