kabanus
kabanus

Reputation: 25895

Gnuplot: How do you load a custom command?

I'm looking for a way to load a custom command to my gnuplot sessions. Often after playing around with a plot I want to output it to PDF, and continue working. This will look like:

set terminal pdf
set output   'somefilename.pdf'
replot
set terminal qt
replot

Currently the best I can do is put that in a separate file with the file name a variable instead of a string, define said variable in my session, then load said file. I'm wondering if I can load this script as a command that takes an argument, so I can do something like

exportpdf "myfile.pdf"

Upvotes: 2

Views: 306

Answers (2)

user8153
user8153

Reputation: 4095

I think your your current method is already pretty good, but if you want you can fine-tune it a little bit:

  1. If you are willing to keep storing the name of the file in a gnuplot variable FILENAME, then you can circumvent the need of an external file by using macros:

    exportpdf="set term push; set term pdf; set output FILENAME; replot; set output; set term pop"
    

    You can then save your current figure by executing

    @exportpdf
    
  2. If you want to give the filename as an argument you can create a script file exportpdf.gp

    set term push
    set term pdf
    set output ARG1
    replot
    set output
    set term pop
    

    and define the string

    exportpdf='call "exportpdf.gp"'
    

    for example in your startup file. Then you can save your current figure to filename simply by executing

    @exportpdf "filename.pdf"
    

Upvotes: 4

ewcz
ewcz

Reputation: 13087

if you want to define a custom "function", you could first construct the appropriate command and then evaluate it:

plotPdf(fname) = eval(sprintf("set terminal pdf;set output '%s';replot;set terminal qt;replot;", fname))

this definition can be then conveniently placed in the Gnuplot startup script so that it is automatically available

Upvotes: 1

Related Questions