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

1 回表示 (過去 30 日間)
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

採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

製品


リリース

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by