3

I'm generating a Gnuplot script in the same folder as a .dat file that I need to plot, and I'd like it to work even if it's called from a different directory. Unfortunately it seems like relative paths work from the directory within which Gnuplot is called, not the one in which the script file is. So for example:

gnuplot folder/script.gp

will fail if the file is folder/data.dat and the script says plot "data.dat". Is there any way to get around this? Like a variable in Gnuplot holding the location of the currently executed script?

2 Answers 2

5

Your separator between directory and filename is /, so I assume you are on Linux. Here the following works:

  • If gnuplot is started with a script for execution, then there is a variable ARG0 which contains the complete script name as it was specified on the command line.
  • Via a system call we can send this script name back to the shell which has a dirname command for extracting the directory part of the script name.
  • Now we can use the gnuplot internal string functions for building file paths, especially the . which concatenates two strings.

You said you have a gnuplot script folder/script.gp and a data file folder/data.txt, then the script should contain something like that:

print ARG0       # just for debugging
working_directory = system("dirname ".ARG0)."/"
datafile = working_directory."data.dat"
outputfile = working_directory."graph.png"

print datafile   # just for debugging
print outputfile # just for debugging

set terminal pngcairo
set output outputfile
plot datafile w l

It also works if you are within "folder", then dirname returns . and the resulting datafile will be ./data.dat. I have not checked 'special' directory names containing spaces, ... I would not expect it to work.

Tested with gnuplot 5.0.5 on Debian 9.6, gnuplot uses the sh shell for system. I have not searched for a Windows solution.

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

2 Comments

Oh, wow, thanks! Kind of convoluted but yes, it works. Too bad for windows though, and I wish the software just added this.
@Okarin I would be optimistic to get this to work for Windows, but have not tried it. Google with something like dirname windows batch returns several matches. I would be more concerned about messing up with whitespace :) But I agree, gnuplot has this information and could provide it independent from operating system.
4
set loadpath "wherever/my/stuff/is/"

This can be either an absolute directory tree or relative to the current directory.

Alternatively, just as you could at the shell command line you could tell gnuplot

cd "/new/current/directory"

after which that will be the first place it looks for files.

3 Comments

The problem with this is I hoped to have a way for it to work independently of whether the entire folder is moved (keeping the same relative path between script and data files, but changing the absolute one). So I was hoping for something akin to Python's __file__ constant.
That's not how it works. Not for gnuplot and not for python either. If you know where the files are at the time you issue your shell command then another alternative is (assuming bash) GNUPLOT_LIB=folder gnuplot script.gp
It absolutely is how it works in Python, at the very least. At any time you can know the path of the current file you're running in. I've used that feature for example to always be sure about the relative position of a data file wrt a script even when I don't know where the package has been installed.

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.