fprintf #22

  • Follow


Hi,

>> str='aaaaa';f=[0.22 0.333];
>> fprintf('%s %f %f\n',str,f)
aaaaa 0.220000 0.333000
>> fprintf('%s %f %f\n',[str,f]')
aaaaa >>
>> [str,f]'

ans =

a
a
a
a
a

I expect to print
'aaaaa'
0.22
0.333

how to do it please?
Mike
0
Reply sulfateion (350) 5/7/2011 2:16:47 AM

On 5/6/2011 9:16 PM, Mike wrote:
> Hi,
>
>>> str='aaaaa';f=[0.22 0.333];
>>> fprintf('%s %f %f\n',str,f)
> aaaaa 0.220000 0.333000
>>> fprintf('%s %f %f\n',[str,f]')
> aaaaa>>
....

> I expect to print
> 'aaaaa'
> 0.22
> 0.333
....

Why would you expect line feeds unless you ask???

fprintf('%s\n%f\n%f\n',str,f)

BTW, repmat() can be wunnerful for building format strings if need many 
multiples of things...or variable numbers of things in the string

--
0
Reply none1568 (6639) 5/7/2011 2:47:51 AM


On May 7, 10:47=A0am, dpb <n...@non.net> wrote:
> On 5/6/2011 9:16 PM, Mike wrote:> Hi,
>
> >>> str=3D'aaaaa';f=3D[0.22 0.333];
> >>> fprintf('%s %f %f\n',str,f)
> > aaaaa 0.220000 0.333000
> >>> fprintf('%s %f %f\n',[str,f]')
> > aaaaa>>
>
> ...
>
> > I expect to print
> > 'aaaaa'
> > 0.22
> > 0.333
>
> ...
>
> Why would you expect line feeds unless you ask???
>
> fprintf('%s\n%f\n%f\n',str,f)
>
> BTW, repmat() can be wunnerful for building format strings if need many
> multiples of things...or variable numbers of things in the string
>
> --

It works. Thanks.
But I don't understand of what you mentioned about repmat() in string
printing.
Can you show an example for me? Many thanks.
Mike
0
Reply sulfateion (350) 5/7/2011 4:09:33 AM

On 5/6/2011 11:09 PM, Mike wrote:
> On May 7, 10:47 am, dpb<n...@non.net>  wrote:
....
>> Why would you expect line feeds unless you ask???
>>
>> fprintf('%s\n%f\n%f\n',str,f)
>>
>> BTW, repmat() can be wunnerful for building format strings if need many
>> multiples of things...or variable numbers of things in the string
....
> But I don't understand of what you mentioned about repmat() in string
> printing.
> Can you show an example for me? Many thanks.
....

Your example is very short so writing '%f\n%f\n' isn't terribly much of 
a problem...that isn't always the case.

Of course, format reversion can help, but not always...

First note the next two cases like your request

 >> sprintf('%s\n%f\n',s,[pi 2*pi])
ans =
aaa
3.141593
6.283185e+000

 >> sprintf('%s\n%f\n',s,[pi 2*pi pi 2*pi])
ans =
aaa
3.141593
6.283185e+000
3.141593
6.283185e+000

Note that the %f for the float is there only once; the string reverts to 
it owing to the two arguments being a string and a vector; the second 
argument is associated w/ the second field in the string.

So, you could have a string and a numeric vector of any length and be 
ok.  But...

 >> sprintf('%s\n%f\n',s,[pi 2*pi],s,[pi 2*pi])
ans =
aaa
3.141593
6.283185e+000
97.000000
aa
3.141593
6.283185e+000

Now it reverts to the %f but also translates the first 'a' of the second 
string argument as numeric before reverting to the beginning of the 
formatting string.

Could write '%s\n%f\n%f\n%s\n%f\n%f\n' out explicitly but that's hard to 
keep the count of stuff straight plus being just plain ugly and thats 
for only a still small output list...so,

 >> sprintf(repmat('%s\n%f\n%f\n',1,2),s,[pi 2*pi],s,[pi 2*pi])
ans =
aaa
3.141593
6.283185
aaa
3.141593
6.283185

repmat() to the rescue.  The counts can be dependent on the size of the 
output list, etc., etc., etc., to make building the list dynamic.

--
0
Reply none1568 (6639) 5/7/2011 1:30:32 PM

3 Replies
36 Views

(page loaded in 0.222 seconds)


Reply: