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

2 views (last 30 days)
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

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 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.

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Products


Release

R2007b

Community Treasure Hunt

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

Start Hunting!