4

I'd like to plot a datafile using substrings from a column. My data file contains data in the format

1 (15, 3): dX: -1.619, dY: 3.315, dXSc: 0.981, dYSc: 0.993
2 ( 4,16): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977
...

and I'd like to plot the numbers between the brackets (x,y) as y against x, something like:

plot "data.dat" u substr(($2),7,8 ):substr(($2),10,11)

What would be the correct syntax for this?

2 Answers 2

5

Doing this properly with gnuplot is a bit tricky, because gnuplot doesn't allow specifying an arbitrary format as input. Usually, the best way would be to use an external tool to extract the data for you and feed the resulting file to gnuplot (this could be also done on-the-fly using the syntax (plot '< script data.dat'...).

However, in your case there is a hack to get it working with gnuplot following the next steps:

  • Use set datafile separator ':' to have the information of both x and y in a single column.
  • Use strstrt to determine the start and end string positions of the x and y values.

    For the x-value this would be

    substr(s, strstrt(s, "(")+1, strstrt(s, ",")-1)
    
  • Add 0.0 to the resulting substrings to have them implicitely converted to real values.

A complete script, which works with your example data is

set datafile separator ":"
get_x(c) = 0.0 + substr(strcol(c), strstrt(strcol(c), "(") + 1, strstrt(strcol(c), ",") - 1)
get_y(c) = 0.0 + substr(strcol(c), strstrt(strcol(c), ",") + 1, strstrt(strcol(c), ")") - 1)

plot 'data.dat' using (get_x(1)):(get_y(1)) with points pt 7 ps 2
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, unfortunately I get the error message: "internal error: substring range operator applied to non-STRING type" My datafile looks like this: 1 (15, 3): dX: -1.619, dY: 3.315, dXSc: 0.981, dYSc: 0.993 2 ( 4,16): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977 ... and I'd like to plot the numbers between the brackets (x,y) as y against x with: plot "data.dat" using (substr(strcol(2),2,3)):(substr(strcol(2),5,6)) What could be the reason for the error ? Thanks
Finally found time to try this, and it's magic: it works. Thank you so much ! Where can I find info about commands like get_(x) ? Regards
Great :) Sticking such functions together require more detailed knowledge about gnuplot's data handling. Gnuplot usually is the wrong tool for doing such parsing, although in some cases like yours it can be done. In many cases it's better to have an external script, e.g. in python or any other language, which does the actual parsing and passes the resulting data to gnuplot, like plot '< python script.py data.dat'.
0

Just for the records: for gnuplot 4.6, Christoph's answer is certainly a good way to go.

However, at the time of OP's question, gnuplot 5.0.0 was already available. gnuplot 5.0.0 (Jan 2015) has the option to define multiple datafile separators, check help datafile separator. With this, the task simplifies to:

Script:

### extract numbers between various separators
reset session

$Data <<EOD
1 (15, 3): dX: -1.619, dY: 3.315, dXSc: 0.981, dYSc: 0.993
2 ( 4,16): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977
3 ( 1.5,10.5): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977
EOD

set datafile separator "(,)"

plot $Data u 2:3 w lp pt 7 lc "red"
### end of script

Result:

enter image description here

Addition:

Actually, you could also set 4 separators, i.e. "(,):" and split your table and write it into a new "clean" table $DataNew. You have to be careful which columns you select, here it would be 1:2:3:6:8:10:12.

Script:

### extract numbers between various separators, get "clean" table
reset session

$Data <<EOD
1 (15, 3): dX: -1.619, dY: 3.315, dXSc: 0.981, dYSc: 0.993
2 ( 4,16): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977
3 ( 1.5,10.5): dX: -0.540, dY: -0.540, dXSc: 0.992, dYSc: 0.977
EOD

set datafile separator "(,):"
set table $DataNew
    plot $Data u 1:2:3:6:8:10:12 w table
unset table
set datafile separator    # set to default

print $DataNew
# plot whatever you like...
### end of script

Result:

$DataNew

 1       15      3       -1.619   3.315  0.981   0.993
 2       4       16      -0.54   -0.54   0.992   0.977
 3       1.5     10.5    -0.54   -0.54   0.992   0.977

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.