gnuplot generate plot command with sprintf?

风流意气都作罢 提交于 2019-12-19 04:14:40

问题


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:


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.



来源:https://stackoverflow.com/questions/21233470/gnuplot-generate-plot-command-with-sprintf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!