input with line break

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

input with line break

Santiago Higuera-2
Hi all:

I'm using Ocatve 5.1, and I've observed that if I use:

x=input("x= \n")

The special character '\n' is recognized, but if I use single quoted
string, it is not recognized.

Is it a bug? In MATLAB single quoted string recognize the special
character for line break in input() function.

Thanks in advance

Santiago Higuera




Reply | Threaded
Open this post in threaded view
|

Re: input with line break

Octave - General mailing list
On 10/3/19 5:12 PM, Santiago Higuera wrote:

> Hi all:
>
> I'm using Ocatve 5.1, and I've observed that if I use:
>
> x=input("x= \n")
>
> The special character '\n' is recognized, but if I use single quoted
> string, it is not recognized.
>
> Is it a bug? In MATLAB single quoted string recognize the special
> character for line break in input() function.

I don't consider it a bug---it's a convention used in many other places
(e.g. the shell)

Matlab disregards escape characters in both forms, but seems to
special-case escape characters (at least \n) in input(). Octave doesn't
need to because you can pass them in double-quoted strings.

It is a minor Octave-Matlab incompatibility but I just think Octave does
it better. Are there other known cases of special-casing escapes in Matlab?



Reply | Threaded
Open this post in threaded view
|

Re: input with line break

Santiago Higuera-2
Thanks, Przemek.  I didn't know.

I read in 'Free your numbers'

5.1 Escape Sequences in String Constants
In double-quoted strings, the backslash character is used to introduce
escape sequences that
represent other characters. For example, ‘\n’ embeds a newline character
in a double-quoted
string and ‘\"’ embeds a double quote character. In single-quoted
strings, backslash is not
a special character.

I read in 'help fprintf':

Implementation Note: For compatibility with MATLAB, escape
      sequences in the template string (e.g., "\n" => newline) are
      expanded even when the template string is defined with single
      quotes.

It's a little confused for me, I don't understand why  single and double
quoted string are different. I don't understand what is the objective or
the advantage of this difference.

Regards
Santiago Higuera

El 4/10/19 a las 22:00, Przemek Klosowski via Help list for GNU Octave
escribió:

> On 10/3/19 5:12 PM, Santiago Higuera wrote:
>> Hi all:
>>
>> I'm using Ocatve 5.1, and I've observed that if I use:
>>
>> x=input("x= \n")
>>
>> The special character '\n' is recognized, but if I use single quoted
>> string, it is not recognized.
>>
>> Is it a bug? In MATLAB single quoted string recognize the special
>> character for line break in input() function.
>
> I don't consider it a bug---it's a convention used in many other
> places (e.g. the shell)
>
> Matlab disregards escape characters in both forms, but seems to
> special-case escape characters (at least \n) in input(). Octave
> doesn't need to because you can pass them in double-quoted strings.
>
> It is a minor Octave-Matlab incompatibility but I just think Octave
> does it better. Are there other known cases of special-casing escapes
> in Matlab?
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: input with line break

BGreen
Here's an example in practice:

>> 'One\nTwo'
ans = One\nTwo

>> "One\nTwo"
ans = One
Two

In the second case, \n is treated as a newline character. In the first, it's treated literally as the two characters \ and n.

A simple example of when it's useful to one versus another is the following: If I want to put newlines in CLI output, I use double quotes so that \n is a newline. On the other hand, if I want to save a directory, then I want \ to be interpreted as an actual backslash character, not the beginning of an escape sequence. See the following example:

>> "C:\Users"
ans = C:Users
>> 'C:\Users'
ans = C:\Users

- Brett Green


On Sun, Oct 6, 2019 at 4:06 AM Santiago Higuera <[hidden email]> wrote:
Thanks, Przemek.  I didn't know.

I read in 'Free your numbers'

5.1 Escape Sequences in String Constants
In double-quoted strings, the backslash character is used to introduce
escape sequences that
represent other characters. For example, ‘\n’ embeds a newline character
in a double-quoted
string and ‘\"’ embeds a double quote character. In single-quoted
strings, backslash is not
a special character.

I read in 'help fprintf':

Implementation Note: For compatibility with MATLAB, escape
      sequences in the template string (e.g., "\n" => newline) are
      expanded even when the template string is defined with single
      quotes.

It's a little confused for me, I don't understand why  single and double
quoted string are different. I don't understand what is the objective or
the advantage of this difference.

Regards
Santiago Higuera

El 4/10/19 a las 22:00, Przemek Klosowski via Help list for GNU Octave
escribió:
> On 10/3/19 5:12 PM, Santiago Higuera wrote:
>> Hi all:
>>
>> I'm using Ocatve 5.1, and I've observed that if I use:
>>
>> x=input("x= \n")
>>
>> The special character '\n' is recognized, but if I use single quoted
>> string, it is not recognized.
>>
>> Is it a bug? In MATLAB single quoted string recognize the special
>> character for line break in input() function.
>
> I don't consider it a bug---it's a convention used in many other
> places (e.g. the shell)
>
> Matlab disregards escape characters in both forms, but seems to
> special-case escape characters (at least \n) in input(). Octave
> doesn't need to because you can pass them in double-quoted strings.
>
> It is a minor Octave-Matlab incompatibility but I just think Octave
> does it better. Are there other known cases of special-casing escapes
> in Matlab?
>
>
>