Wed 08 Apr 2015 04:49:37 AM UTC, comment #10:
Fixed in this cset (http://hg.savannah.gnu.org/hgweb/octave/rev/dbf2418a46dd).
|
Tue 07 Apr 2015 02:52:23 PM UTC, comment #9:
I proposed
"Beware that these functions will interpret the backslashes in the template string, even if this is a single quote string"
after
"The following functions are available for formatted output. They are modeled after the C language functions of the same name, but they interpret the format template differently in order to improve the performance of printing vector and matrix values."
in
https://www.gnu.org/software/octave/doc/interpreter/Formatted-Output.html
|
Tue 07 Apr 2015 02:40:05 PM UTC, comment #8:
Yes, it should probably be documented. Can you suggest where, and what wording to use?
|
Tue 07 Apr 2015 02:26:25 PM UTC, comment #7:
@jwe, agreed, and using %s is a great idea, very clean. Thanks !
But current beaviour can be surprising, shouldn't it be documented somewhere ?
This is why I chose "incorrect documentation" as item group,
and proposed a complement for the documentation,
at the end of the original submission.
|
Tue 07 Apr 2015 11:48:32 AM UTC, comment #6:
OK, I think this came up before, and once I saw that Matlab produced a warning and completely dropped the escape sequence, I decided to not do anything about it.
You can write something like
if you want to be sure that an escape sequence won't be processed. This is generally what you will have to do anyway if you want to insert things like \theta because I don't see a way to guess whether you really want \theta or a tab followed by 'heta'.
|
Tue 07 Apr 2015 04:04:09 AM UTC, comment #5:
>> s = sprintf('\z')
Warning: Control Character '\z' is not valid. See 'doc sprintf' for control characters valid in the format string.
s =
Empty string: 1-by-0
>> version
ans =
8.5.0.197613 (R2015a)
|
Tue 07 Apr 2015 01:28:04 AM UTC, comment #4:
I agree it would be good to get confirmation on what Matlab's sprintf/fprintf does with invalid backslash escape sequences. What does Matlab produce with
I looked through the manual and the backslash string escapes are not mentioned anywhere in the section "C-Style I/O Functions". They are documented elsewhere as a feature of double-quoted strings only. So I can understand the confusion if someone is not familiar with the printf family of functions and that this is a normal expected feature.
|
Mon 06 Apr 2015 08:09:13 PM UTC, comment #3:
@Rik, Thanks for your support. I did propose a doc change in the end of the original submission. Do you mean that a patch is required, or that it is not clear enough ?
@jwe, that would be nice, provided that '\nu' and '\theta' are handled correctly as well, when the sprintf result is passed to the tex interpreter, as in
title(sprintf('\nu = %.1f', 0.1))
and
title(sprintf('\theta = %.1f', 0.1))
|
Mon 06 Apr 2015 06:50:38 PM UTC, comment #2:
When transforming escape sequences in single-quote strings that appaear in *printf formats, maybe we should just transform the escape sequences that Matlab recognizes and silently ignore the rest, perserving the backslash? If that's what Matlab does, then we should probably do the same.
|
Mon 06 Apr 2015 06:14:18 PM UTC, comment #1:
Coming from a UNIX shell programming environment I hate this as well. Matlab, however, only has the single quote character for all strings. So for Matlab compatability, Octave expands escape sequences like "\n" or "\t"--even within single quotes--if the function is sprintf, printf, regexp, regexprep, and probably scanf too.
Can you suggest a documentation change that would be clear about this?
|
Mon 06 Apr 2015 05:37:53 PM UTC, original submission:
yields
This is unexpected, since the documentation for strings states that
"In single-quoted strings, backslash is not a special character"
And the sprintf documentation does not appear to mention anything about that:
"This is like printf, except that the output is returned as a string. Unlike the C library function, which requires you to provide a suitably sized string as an argument, Octave’s sprintf function returns the string, automatically sized to hold all of the items converted."
Is it a bug, or is it done on purpose ?
I infer the latter, from a comment from Soren:
"deviate from this rule just as the '*printf' functions do."
Then, after
"The following functions are available for formatted output. They are modeled after the C language functions of the same name, but they interpret the format template differently in order to improve the performance of printing vector and matrix values."
something like
"Beware that these functions will interpret the backslashes in the template string, even if this is a single quote string"
could be helpful ?
Note this happens also under 3.8.2.
|