fprintf compatibility

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

fprintf compatibility

John W. Eaton-6
What do you think about this feature of Matlab?

  >> fprintf ('%s\n', [111; 100; 100]);
  odd

I know that Matlab (originally, anyway, though perhaps not anymore)
stores strings as double precision matrices with a flag set saying to
interpret the numbers as ASCII and print them as strings, etc.  But
the matrix here is not even tagged as a string!

So, should Octave copy this bug^H^H^Hfeature?

jwe


Reply | Threaded
Open this post in threaded view
|

Re: fprintf compatibility

Paul Kienzle
John W. Eaton wrote:

>What do you think about this feature of Matlab?
>
>  >> fprintf ('%s\n', [111; 100; 100]);
>  odd
>
>I know that Matlab (originally, anyway, though perhaps not anymore)
>stores strings as double precision matrices with a flag set saying to
>interpret the numbers as ASCII and print them as strings, etc.  But
>the matrix here is not even tagged as a string!
>
>So, should Octave copy this bug^H^H^Hfeature?
>
Won't this happen already with implicit_num_to_str_ok = 1?

Okay, no it doesn't.

Even if it did, shouldn't it output the following:

o
d
d

I have nothing in particular against implicit char->num/num->char, but I
also don't
mind putting setstr() around the matrices.  I'm a little disappointed that
sprintf('%s\n',['o';'d';'d']) doesn't print a column though.

- Paul


Reply | Threaded
Open this post in threaded view
|

Re: fprintf compatibility

John W. Eaton-6
On 31-Dec-2002, Paul Kienzle <[hidden email]> wrote:

| John W. Eaton wrote:
|
| >What do you think about this feature of Matlab?
| >
| >  >> fprintf ('%s\n', [111; 100; 100]);
| >  odd
| >
| >I know that Matlab (originally, anyway, though perhaps not anymore)
| >stores strings as double precision matrices with a flag set saying to
| >interpret the numbers as ASCII and print them as strings, etc.  But
| >the matrix here is not even tagged as a string!
| >
| >So, should Octave copy this bug^H^H^Hfeature?
| >
| Won't this happen already with implicit_num_to_str_ok = 1?
|
| Okay, no it doesn't.

Right, but perhaps it should.

| Even if it did, shouldn't it output the following:
|
| o
| d
| d
|
| I have nothing in particular against implicit char->num/num->char, but I
| also don't
| mind putting setstr() around the matrices.

Setstr is definitely the quick fix.

| I'm a little disappointed that
| sprintf('%s\n',['o';'d';'d']) doesn't print a column though.

Disappointing or not, that part is already compatible.  :-/  But it
does seem inconsistent with the behavior of

  fprintf ('%d\n', [1; 2; 3])

for example.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: fprintf compatibility

Alois Schlögl-2
In reply to this post by John W. Eaton-6

> On 31-Dec-2002, Paul Kienzle <[hidden email]> wrote:
>
> | John W. Eaton wrote:
> |
> | >What do you think about this feature of Matlab?
> | >
> | >  >> fprintf ('%s\n', [111; 100; 100]);
> | >  odd
> | >
> | >I know that Matlab (originally, anyway, though perhaps not anymore)
> | >stores strings as double precision matrices with a flag set saying to
> | >interpret the numbers as ASCII and print them as strings, etc.  But
> | >the matrix here is not even tagged as a string!
> | >
> | >So, should Octave copy this bug^H^H^Hfeature?
> | >
> | Won't this happen already with implicit_num_to_str_ok = 1?
> |
> | Okay, no it doesn't.
>
> Right, but perhaps it should.
>
> | Even if it did, shouldn't it output the following:
> |
> | o
> | d
> | d
> |
> | I have nothing in particular against implicit char->num/num->char, but I
> | also don't
> | mind putting setstr() around the matrices.
>
> Setstr is definitely the quick fix.
>
> | I'm a little disappointed that
> | sprintf('%s\n',['o';'d';'d']) doesn't print a column though.
>


There is a difference between strings and chars.

A column vector is printed with flag %c

>> sprintf('%c\n',['o';'d';'d'])

ans =

o
d
d


sprintf('%s\n',['o';'d';'d'])

ans =

odd




- Alois



> Disappointing or not, that part is already compatible.  :-/  But it
> does seem inconsistent with the behavior of
>
>   fprintf ('%d\n', [1; 2; 3])
>
> for example.
>
> jwe
>
> --
>
>


--