2

I want to generate a gnuplot plot command programmatically, like:

plotline = sprintf("'datafile1.dat' using %d:3 with points, '%s' using %d:3 with points",i,targfile,i)
plot plotline

Where 'plotline' in the second line is expanded to produce and execute a full command like:

plot 'datafile1.dat' using 8:3 with points, 'datafile2.dat' using 8:3 with points

I want to do this in order to echo 'plotline' in the terminal and so be certain exactly what is being shown while cycling through a set of columns / datafiles / whatever inside a loop in a gnuplot script.

Is there / what is the syntax to do this, or can you suggest another approach to report the plot command as executed (without splitting into a plot command and a separate set of commands to report the current variable states).

Thanks!

1

1 Answer 1

2

In order to construct such a plot command from some strings, you can use eval to execute the commands contained in a string:

plotline = 'x title "mytitle"'
eval('plot '.plotline)

Alternatively you can use set macros:

set macros
plotline = 'x title "mytitle"'
plot @plotline

This replaces @plotline with the content of the string variable plotline before executing the command. Using plot plotline interpretes the content of plotline as file name. Note, that as of version 4.6 macros don't work properly in loops, but eval works fine.

BTW: If you don't specify your own title, then the actual plot statement is written in the plot legend. But that can't be written to the terminal output.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks! was unable to make 'set macros ... @plotline' work for me as plotline from sprintf is 'not a string variable', however eval() does exactly what I was after.
Thats strange. For me, with 4.6.3, both the following statements work fine: set macros; plotline = sprintf("%f*x", 4); plot @plotline and also set macros; plotline = sprintf("'%s' using 0:1", 'test.txt'); plot @plotline (of course provided, that you have such a file test.txt.
I would add that with the current 4.6 version macros within a for loop don't work as intended, so the eval version is to be preferred.
@Andreas Thanks for the remark, I put the eval part first and added a note related to macros and looping.

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.