8

Actually, I get this problem in Gnuplot. But in a simple way, the problem is I want to use sprintf to print in string format, even though the string consists of numbers and symbol. Like this:

a = '12/7'

sprintf('%.f', a)

I am able to run it in Gnuplot, but it only shows numbers before symbol "/". I expect the output should print all the content of variable a which is '12/7'. Any suggestion for this case?

0

3 Answers 3

9

If you are referring to the sprintf built-in function in gnuplot, the documentation help sprintf says:

sprintf("format",var1,var2,...) applies standard C-language format specifiers to multiple arguments and returns the resulting string. If you want to use gnuplot's own format specifiers, you must instead call gprintf(). For information on sprintf format specifiers, please see standard C-language documentation or the unix sprintf man page.

In the Unix man page for %f says (truncated):

%f, %F -- The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification.

Hence, you need to use a different format specifier. As fedorqui pointed out the appropriate format specifier is %s.

I often refer to the table in this link for looking up the appropriate format specifiers (I find the C man page a bit long).

Using sprintf with strings from files

In order to use sprintf to format a string from a file, you need to specify the column in the file to extract the string information from. This needs to be done using the stringcolumn command instead of just using a $ sign.

For example, if you wanted to include a number from a file you might use a command like:

plot "data.dat" using 1:2:(sprintf('%f', $3)) with labels

But for a string you need:

plot "data.dat" using 1:2:(sprintf('%s', stringcolumn(3))) with labels
Sign up to request clarification or add additional context in comments.

6 Comments

thanks you for the answer @ilent2, I have tried %s but it shows an error f_sprintf: attempt to print numeric value with string format do you have any idea for that?
@indi60 Are you sure the variable is a string? When I run gnuplot with gnuplot> a = '3.5' and gnuplot> print(sprintf('%s hi', a)) it works fine for me. Could you edit your original question with the exact code your using.
here is my plot file plot "<awk '{$1=\"\"}1' '1q.dat' | sed '1 d'" matrix every 2::1 w image, \ '' matrix using ($1+1):2:(sprintf('%.f', $3)) every 2 with labels
actually you can see my input and whole plot file in this.. stackoverflow.com/questions/34504450/…
@indi60 So, to fix your plot command try: plot "<awk '{$1=\"\"}1' '1q.dat' | sed '1 d'" matrix every 2::1 w image, \ '' matrix using ($1+1):2:(sprintf('%s', stringcolumn(3))) every 2 with labels. Hopefully this helps.
|
3

When you say

$ awk 'BEGIN {a="12/7"; printf "%.f", a}'
12

You get the int part of the number, because by using a f flag in printf you are casting it to a number instead of a string. So it is equivalent to saying:

awk 'BEGIN {a="12/7"; a=int(a); print a}'

If you want to keep all of it, use %s for string:

$ awk 'BEGIN {printf "%s", "234-456"}'
234-456

Comments

0

You can multiply by floating-point number and use that as following: sprintf('%.f', a*1.0)

In this way, you force gnuplot to treat a as floating-point number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.