2

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"
3
  • you can define custom commands which are loaded on startup - stackoverflow.com/a/1200530/5351549 Commented Dec 13, 2017 at 11:28
  • @ewcz where does it say there how to define a custom command? The at init part is less important. Commented Dec 13, 2017 at 11:44
  • stackoverflow.com/a/30334657/2604213 Commented Dec 13, 2017 at 19:19

2 Answers 2

4

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"
    
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

I'm getting invalid command when trying plotPdf no arguments, plotPdf('something') or plotPdf 'something'.
I'm GnuPlot version 5.0 patch level 5 if it matters.
@kabanus and could you try to execute directly set terminal pdf;set output '%s';replot;set terminal qt;replot; with %s replaced with the argument you tried before? just to see which part fails...
I wasn't clear - the error is is that plotPdf itself is invalid. Your line itself is fine. I tried print plotPdf('abc'), but then it complains eval is not a function, though it works as is when input directly to the command line -eval(sprintf("set terminal pdf;set output '%s';replot;set terminal qt;replot;", 'abc')) works directly.
x() = eval(sprintf("set terminal pdf;set output '%s';replot;set terminal qt;replot;", 'abc')); complains x is an invalid command, but x = eval(sprintf("set terminal pdf;set output '%s';replot;set terminal qt;replot;", 'abc')); complains eval is an undefined function.

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.