I'm trying to save an .eps file in a folder that doesn't exist yet. I wrote the following gnuplot script:
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output 'output/x=-4.eps'
replot
The script only works if the "output" folder is already there . Can I create this folder with gnuplot?
Thanks.
You can use the system command:
system "mkdir output"
on Linux, I seem to remember that it is md output on Windows.
Combined with string operators, this is rather flexible:
dir = "output1"
command = "mkdir " . dir
system command
works nicely.
You can concatenate the name of the output file with the folder name:
folderout = 'output/'
plot [0:0.13][0:55] 'example/x_-4_U.xy'
set output folderout.'x=-4.eps'
replot
Sincerely.
Related
I fount a strange phenomenon. If we run the following gnuplot script (in a folder with gnuplot-lua-tikz-common.tex, gnuplot-lua-tikz.sty, gnuplot-lua-tikz.tex, t-gnuplot-lua-tikz.tex)
tikzfile="test.tex"
plot x**2
set term tikz standalone monochrome
set output tikzfile #
replot #
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
we found the following fatal error
! Emergency stop.
<*> test.tex
! ==> Fatal error occurred, no output PDF file produced!
Anyway we have the test.tex file. Hence if we re-run the same script with the #-marked lines commented we obtain no error and the perfect test.pdf file.
During the first exec, with set term we have an empty file, with replot we fill it, but until the end of the exec we can't use it as an input of pdflatex. Why?
During the second exec we already have the test.tex file, so if we comment set term and replot we can use it as an input of pdflatex. Why?
Thank you.
Gnuplot doesn't automatically flush and finish an output file after a plot.
So, if you want to further process an output file from within the gnuplot script you must explicitly close the file with set output beforehand:
tikzfile="test.tex"
set term tikz standalone
set output tikzfile
plot x**2
set output
cmd="pdflatex -interaction=nonstopmode ". tikzfile
system(cmd)
In my bash script, I am doing a bunch of operations before starting my gnuplot script. My bash script looks like this:
#!/bin/bash -e
# Do some bash operations here
# Create a file containing a list of Gnuplot commands called arrow_coords
# All the commands are the same: just drawing a group of arrows
# Every line in "arrow_coords" looks like this:
# set arrow from -500,-100 to 500,-100 as 1
# There are hundreds of those line commands
# Entering Gnuplot script
gnuplot <<- EOF
reset
set terminal pngcairo transparent nocrop enhanced font '$font_type, 22' size 1800,1800
set output "$output_file"
set yrange [-1:18]
set xrange [-1:18]
? <----------------------------------- HOW TO INSERT COMMANDS FROM ANOTHER FILE?
plot '$dat_file' using 1:2 with points ls 1
EOF
I could not find a way to insert the commands written in arrow_coords into the Gnuplot script I have in my bash file. Is this possible? Are there other suggestions to what I am trying to do?
If your file contains only gnuplot instructions, you can run it with the load or call commands:
gnuplot <<- EOF
# your gnuplot configurations
load 'arrow_coords' # calling external file
plot '$dat_file' using 1:2 with points ls 1
EOF
Here's an example that illustrates the solution:
#!/bin/bash
# prepare file
echo "Test!" > test.txt
a=`cat test.txt`
cat <<- EOF
File contents: $a
Again: `cat test.txt`
EOF
So in your code, you could replace the line starting with ? with:
`cat the_file_you_generated`
How to programmatically get the name of current gnuplot script? I know that I can call gnuplot script from bash and get it file name but I am wondering if it is possible from inside gnuplot. My goal is to make something like:
date=system("date +%F_%T | sed 's/:/-/g'")
my_name=$0 # THIS IS HOW TO DO IT IN BASH
set term png
set output my_name.date.".png"
I've tried:
my_name=system("cat /proc/$$/cmdline")
but it returned sh instead of script name
Not quite an answer to your question, but this might help with what you want to do:
You can leave my_name unset in the script, and set it either inside gnuplot, just before you load the script (where you need to know the script name anyway):
my_name=...
load(my_name)
or set it when you invoke gnuplot from the shell:
$ gnuplot -e "my_name=${FILE}" ${FILE}
A few more things:
date=system("date +%F_%T | sed 's/:/-/g'")
can be replaced with
date=system("date +%F_%H-%M-%S")
(which is shorter and doesn't need to be parsed through sed) or without any forking at all:
date=strftime("%F_%H-%M-%S",time(0.0))
Using gnuplot version 5 you have access to the file called with load via the variable ARG0
Consider the script test.gp which contains only
print ARG0
Now, calling this with
gnuplot -e "load 'test.gp'"
prints you test.gp on the screen. With earlier versions you don't have access to a similar variable (also not when using call). For earlier versions you must stick to one of the solutions given by #chw21
I need to run gnuplot scripts on different devices. The file structure is the same on the devices, once I get to a particular directory, but the paths to those directories are different. I think I need to have a variable in the path name that I can change according to which device I am on.
I thought the following would work, because I found something similar here, but it doesn't:
path_to_directory="/desktop/path/to/directory"
# path_to_directory="laptop/path/to/directory"
# the line above is commented out but it can be included
# if I want to switch from my desktop to my laptop
path_to_file="$path_to_directory/path/to/file"
plot path_to_file ...
A warning message says: Skipping unreadable file "$path_to_directory/path/to/file"
Do you know how I can include a variable in the path of a gnuplot script, so that I can switch easily between paths?
Thank you.
In gnuplot you don't use $ to indicate variables as you do in bash. You would do a concatenation operation with a dot:
path_to_directory="/desktop/path/to/directory"
path_to_file=path_to_directory."/path/to/file"
You simply need to concatenate two string variables with the . operator:
path_to_directory = "/desktop/path/to/directory"
path_to_file = path_to_directory . "/path/to/file"
plot path_to_file ...
You can also define a default path which can be overridden:
default_path_to_directory = "/desktop/path/to/directory"
if (!exists("path_to_directory")) path_to_directory = default_path_to_directory
path_to_file = path_to_directory . "/path/to/file"
plot path_to_file ...
Now you can override the path_to_directory but must not: e.g. when calling gnuplot like gnuplot -e "path_to_directory='...'" script.gp or with a configuration file.
I know I can plot with data from stdin by using '-' as the data source, but is there any way I can plot data output from a command the same way? I.e., instead of running the command and piping to the gnuplot script, can I specify the command in the gnuplot script itself?
Something like this (but this doesn't work):
set terminal pngcairo
set output 'test.png'
cmd = "`./mycmd`" # running ./mycmd in terminal gives my data to stdout.
# the command can be several other commands, piped together.
# i'm only interested in whatever comes to stdout after running
# the entire thing.
plot cmd u 2:3 w lines # etc...
The above makes cmd contain a single long line with all the lines of output smashed together.
Yes, you can:
plot '< ./mycmd'