1

I'm having a hard time getting my gnuplot script to work. Basically, I have six data files and I want to plot them in the same file with a fit for every data set.

It gets a little complicated because I have to reformat the data before plotting. For that I am using stats commands. Here lies the problem. The final plotting command contains a for loop in which the data formatting is done. Each stats command produces statistical data with a prefix Potentiali, where i goes from 1 to 6.

Now my question is, how can I access this running index in the plot loop?

This is my script:

#!/bin/bash

gnuplot << EOF

set terminal epslatex color size 16cm,11cm
set output "strain-energy.tex"

set xrange [-10:10]
set yrange [0:*]


fstr(N) = sprintf('f%d(x) = a%d*x**7 + b%d*x**6 + c%d*x**5 + d%d*x**4 + e%d*x**3 + f%d*x**2 + g%d*x + h%d', N, N, N, N, N, N, N, N, N)
eval(fstr(1))

fitstr(N) = sprintf('set fit quiet; fit [-10:10] f%d(x) ''/path/Shift_%d/potential.dat'' every ::1 using (\$1-Potential%d_pos_min_y):(\$2-Potential%d_min_y) via a%d,b%d,c%d,d%d,e%d,f%d,g%d,h%d', N, N, N, N, N, N, N, N, N, N, N, N)

do for [i=1:6] {
    stats "/path/Shift_".i."/potential.dat" every ::1 using (\$1):(\$2) prefix "Potential".i nooutput
    eval(fstr(i))
    eval(fitstr(i))
}

plot for [i=1:6] "/path/Shift_".i."/potential.dat" every ::1 using (100*((\$1-Potential.i._pos_min_y)/Potential_pos_min_y)):(1000*(\$2-Potential_min_y)) ls i title "\\\footnotesize{C".i."}", f1(x) ls 1, f2(x) ls 2, f3(x) ls 3, f4(x) ls 4, f5(x) ls 5, f6(x) ls 6

set output
EOF
5
  • I'd really appreciate it, if somebody had the time to look into this, as I'm currently approaching a deadline. Commented Sep 29, 2014 at 16:43
  • 1
    It would be helpful if you removed the unnecessary code and focus on the problem, so any user willing to help doesn't get discouraged. This is a somehow related question (stackoverflow.com/questions/23671742/…), you can store your values in a list and then access the elements using the word() function. Commented Sep 29, 2014 at 18:02
  • I know, I usually don't post questions of this quality but I'm really in a hurry. I cleaned up my script a little. Hope that helps. Commented Sep 29, 2014 at 18:21
  • If you are really in a hurry, my advice is to just serialize your script. Instead of doing things in a for loop, copy and paste the relevant stats line six times and change what you need to, then plot '/path/Shift_1/potential.dat' ... . Keeping the loops would likely require fiddling either with word() as @Miguel mentioned, or eval which will lead you down a dark and twisted road... Commented Sep 29, 2014 at 18:26
  • That should be easy. Thought about that in the beginning, but haven't considered it since running into these problems. It makes for an ugly script but I think I'm past caring. Commented Sep 29, 2014 at 19:17

1 Answer 1

2

In newer gnuplot versions use the do for construction to create a string with all the values you need and then use the word() function to select the one you want. If your variables are a1, a2 and a3 (change to the name in your particular case) then do:

a1=1.1; a2=2.2; a3=3.3 # Values
a="" # a is the string where all the values are stored
do for [i=1:3] {eval "a = sprintf(\"%s %g\", a, a".i.")"} # Print a1, etc. to a
print a # Just check what a looks like
  1.1 2.2 3.3
a(i) = real(word(a,i)) # Create function that depends on i
print a(1) # Check that a(1) indeed gives a1
 1.1

You'll need to adapt the above code to your purposes.

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

Comments

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.