Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #1155

Re: How to print floats with leading blanks instead of leading zeros using sprintf?

From valerasimonov@gmail.com
Newsgroups comp.graphics.apps.gnuplot
Subject Re: How to print floats with leading blanks instead of leading zeros using sprintf?
Date 2012-05-28 20:19 -0700
Organization http://groups.google.com
Message-ID <d3fa6064-db5c-4ce9-af48-0469dad51c2e@googlegroups.com> (permalink)
References <3d97629a-6d13-4e42-bb08-9a1d05a569d9@vy7g2000pbc.googlegroups.com> <jq0fds$lrn$1@dont-email.me>

Show all headers | View raw


On Monday, May 28, 2012 2:16:26 PM UTC-4, sfeam wrote:
> Валерий Симонов wrote:
> 
> > Hi everyone,
> > 
> > I want to specify labels for the plots using sprintf.
> > According to
> > http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ sprintf('
> > %3.0f',1) should print "001"
> > sprintf(' %3.0d',1) should print "  1".
> > However gnuplot outputs:
> > sprintf(' %3.0f',1)  "001"
> > sprintf(' %3.0d',1) "1" (without leading blanks).
> > 
> > Does anyone know how to fix this issue?
> 
> I cannot reproduce either of those results here.
> The first one in particular makes no sense.
> Could it be that you are not really using the format you show us?
> 
>  gnuplot> print sprintf("|%3.0d|",1)
>  |  1|
>  gnuplot>
> 
> 
> > 
> > Thanks in advance!
> > 
> > Regards,
> > Valera

sfeam, thank you for your reply.

I will provide some more details.

I want to plot the file:
"sprintf.txt"
0.2	1.5970E-01	1.1262E-01
0.4	5.7188E-01	2.2227E-01
0.6	2.2019E-01	4.3648E-01

executing the following script: ./plot_sprintf 20 1
#!/bin/sh
gnuplot << EOF
set terminal postscript eps enhanced
set output 'plot_sprintf.eps'
plot "sprintf.txt" using (\$1):(\$2) w l ls 1 t sprintf('T > %03.0f  abc',$1),\
     "sprintf.txt" using (\$1):(\$3) w l ls 2 t sprintf('T > %3d  abc',$2)
unset table
EOF

After the script execution I get an eps file with the following strings that "don't align properly": they look approximately like this
T > 020 abc
 T >  1 abc
instead of looking like this (the way I want):
T > 020 abc
T >   1 abc.

As Hans-Bernhard mentioned I used %d specifier for printing a float, which is of course wrong. Actually all the "floats" I provide for sprintf can be converted into integers (so these floats are 20.0 and 1.0 in the above example). I used %d because I started trial and error method just to get what I want. 

P.S.
In the reference it was written:
"Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger."
So specifying sprintf('T > %3.0f  abc', 1) I expected to see desired blanks, but I didn't. Than I started applying "everything you can try" method.
By the way sprintf('T > %3.0f  abc', 1) did the same job as  sprintf('T > %3d  abc', 1). However  sprintf('T > %5d  abc', 1) had the desired output form
T > 020 abc
T >   1 abc
but only for 1 digit numbers.

Sorry, I did a pretty bad job writing my first post.

Back to comp.graphics.apps.gnuplot | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to print floats with leading blanks instead of leading zeros using sprintf? Валерий Симонов <valerasimonov@gmail.com> - 2012-05-28 08:09 -0700
  Re: How to print floats with leading blanks instead of leading zeros using sprintf? sfeam <sfeam@users.sourceforge.net> - 2012-05-28 11:16 -0700
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? Валерий Симонов <valerasimonov@gmail.com> - 2012-05-28 19:33 -0700
      Re: How to print floats with leading blanks instead of leading zeros using sprintf? sfeam <sfeam@users.sourceforge.net> - 2012-05-28 20:48 -0700
        Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-29 06:18 -0700
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-28 20:19 -0700
  Re: How to print floats with leading blanks instead of leading zeros   using sprintf? Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-05-28 23:32 +0200
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-28 19:56 -0700
      Re: How to print floats with leading blanks instead of leading zeros   using sprintf? Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-05-29 18:33 +0200
        Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-31 05:43 -0700
  Re: How to print floats with leading blanks instead of leading zeros using sprintf? Christoph Bersch <usenet@bersch.net> - 2012-05-29 09:51 +0200
    Re: How to print floats with leading blanks instead of leading zeros using sprintf? valerasimonov@gmail.com - 2012-05-29 06:19 -0700

csiph-web