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
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 CenterFile Exchange 中查找有关 Numeric Types 的更多信息

产品


版本

R2007b

Community Treasure Hunt

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

Start Hunting!

Translated by