Reputation: 150
I have a file with multiple datasets that looks something like this:
# Isochrone Z = 0.00800 Age = 1.000e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
...
# Isochrone Z = 0.00800 Age = 1.122e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
...
I need to be able to use the index feature, like this:
plot 'file' index n u 1:2
so I can plot the n-th dataset, or a range of datasets (index a:b
), but I get an x range is invalid
error. Probably because the commented lines are not being treated as two blank lines. Could this be fixed?
Also, would it be possible to include in the title of the plot the Age value?
Upvotes: 1
Views: 126
Reputation: 7627
From the gnuplot help: "Data sets are separated by pairs of blank records". I am guessing you are not using blank lines as data set separators. If your file looks like this:
# Isochrone Z = 0.00800 Age = 1.000e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
# Isochrone Z = 0.00800 Age = 1.122e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
Then the following bash command can add the blank lines without the need to manually edit the file's content:
user@machine:~$ sed 's/# Isochrone/\n\n# Isochrone/g' file
# Isochrone Z = 0.00800 Age = 1.000e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
# Isochrone Z = 0.00800 Age = 1.122e+07 yr
# Mu Mb Mv Mr Mi Mj Mh Mk Flum
14.982 13.538 12.020 11.076 10.083 9.024 8.401 8.172 -9.59274740
13.741 12.481 11.074 10.195 9.366 8.378 7.710 7.510 -6.50542581
The command above inserts two blank lines before # Isochrone
every time it finds it. And now you can use the command inside gnuplot:
plot "< sed 's/# Isochrone/\\n\\n# Isochrone/g' file" index 0 u 1:2
Note you need to escape backslashes within gnuplot. To get the age you can use a similar approach with a system call within gnuplot:
n = 1 # First record
age_1 = system("awk '/Age/{i++}i==" . n . "{print $(NF-1); exit}' file")
print age_1
1.000e+07
Or get all of them at the same time and store them in a string:
age = ""
do for [n = 1:2] {
age = age . " " . system("awk '/Age/{i++}i==" . n . "{print $(NF-1); exit}' file")
}
print age
1.000e+07 1.122e+07
Now you can conveniently use the same index for index
and the title:
plot for [i=1:2] "< sed 's/# Isochrone/\\n\\n# Isochrone/g' file" \
index (i-1) u 1:2 title "Age = " . word(age,i) . " years"
Are you studying magnetic flux in stars?
Upvotes: 1