sprintf format specifiers won't print newline

31 ビュー (過去 30 日間)
Frances
Frances 2012 年 8 月 3 日
I'm trying to create a string that has a newline in it. My problem is with sprintf. I'm trying to put a newline (\n) character in my string, and it's behaviour is very strange... sometimes sprintf returns an empty string, and sometimes it just prints '\n' as part of the text. I want it to actually insert a line.
This is sort of what my string looks like, but with some variables inserted between the angle brackets:
entry = '% * < >\n';
I tried going straight ahead with sprintf:
sprintf(entry)
This produces
Empty string: 1-by-0
I tried accounting for the newline:
sprintf('\n', entry)
This seems to produce just a newline. I then thought maybe the percent sign was special, so I tried the 'escape character' syntax which is mentioned on the sprintf reference page but not really explained:
sprintf('%%', entry)
This produces just a percent sign. Next I tried using the string specifier %s. I found that
sprintf('%s', entry)
produced
% * < >\n
and that
sprintf('%s\n', entry)
produced the same thing but with a newline after the string, but not where my actually '\n' is. Whenever I add '%%' in there, it just prints out a percent sign. Can anyone tell me what I'm doing wrong? This is puzzling because simply doing sprintf(string) works for me with other strings that have newlines in them.
  1 件のコメント
Frances
Frances 2012 年 8 月 3 日
Do you mean as the format specifier? Because that returns an empty string.

サインインしてコメントする。

採用された回答

Frances
Frances 2012 年 8 月 3 日
編集済み: Frances 2012 年 8 月 3 日
It turns out that '%' needs to be written as '%%' in the string (thank you Honglei Chen), and that was somehow interfering with my newline working as well. In the end I used
entry = sprintf('%%* < >\n');
which produces
% * < >
plus a newline. If you use '%s', I guess it interprets every character as itself:
entry = sprintf('%s', '%%* < >\n');
ans =
%%* < >\n
  1 件のコメント
Image Analyst
Image Analyst 2012 年 8 月 3 日
Yes. See my answer. sprintf has the first string argument as the format specifier. Everything after that are your variables that it's going to try to insert into your string according to any specifiers you put in there. specifiers start with the percent symbol. So you did %s and it's going to try to stick your next argument, '%% * < >\n' in place of the %s - that's how sprintf operates. However, as you eventually found out, you can have a string without any variables embedded and with just special characters like \n if you just have one string argument.

サインインしてコメントする。

その他の回答 (2 件)

Honglei Chen
Honglei Chen 2012 年 8 月 3 日
% and \ are escape characters so you need to treat them special, try
sprintf('%%*<>\\n');
You can find this information in the doc
doc sprintf
or
help sprintf
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 3 日
the result is
%*<>
Honglei Chen
Honglei Chen 2012 年 8 月 3 日
There is a new line if you look carefully

サインインしてコメントする。


Image Analyst
Image Analyst 2012 年 8 月 3 日
編集済み: Image Analyst 2012 年 8 月 3 日
It's just a regular sprintf() like you're used to with other languages:
outputString = sprintf('%s\n%d\n%f', myString, myInteger, myDouble);
Simply put in the specifier %s, %d, etc. for the variable you want to insert, then put in \n whenever you want your string to start on a new line. For example:
myString = 'Hello Frances';
myInteger = 42;
myDouble = pi;
outputString = sprintf('%s\n%d%%\n%f', myString, myInteger, myDouble)
In the command window you'll see:
outputString =
Hello Frances
42%
3.141593
Note, each is on a new line.

カテゴリ

Help Center および File ExchangeBoard games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by