Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use substrings in gnuplot

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?

like image 241
collin Mcdavids Avatar asked Oct 24 '25 15:10

collin Mcdavids


1 Answers

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
like image 80
Christoph Avatar answered Oct 27 '25 19:10

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!