2

I have csv-output from another program like this:

1.0s,34.56a
3.0s,352.12a
5.0s,1995.34b

The first column contains x-values (the s can be neglected) while the second column contains y-values. Using Gnuplot 5 it is possible to simply plot the values, however, I need to determine the leading sign of each value in the second column (all values followed by a should be positive, all values followed by b should be negative).

While the determination of the sign should be easy like described in this post, I'm struggling to split each y-value dynamically. According to the Gnuplot manual, using a substring should be possible but I can't figure out how to tell the substring to only contain the last value.

Community
  • 1
  • 1
PSC
  • 205
  • 1
  • 7

2 Answers2

3

To get the last character of a string s, use

s[strlen(s):]

To parse your data file correctly, use the following:

set datafile separator ','
parse(s) = (s[strlen(s):] eq "b" ? -1 : 1) * real(s[:strlen(s)-1])
plot 'data.dat' using 1:(parse(strcol(2)))
Christoph
  • 47,569
  • 8
  • 87
  • 187
0

I just solved it, posting my solution for others. My solution is based on this answer and this answer.

I created a new function customydata(s)=(s[strlen(s):*]) which returns the last character of a string.

The plot command simply is plot 'heelingdata.dat' u 1:(customydata(strcol(2)) eq 'a' ? (-1)*$2 : $2) w lp where I insert my string into the abovementioned function and check if it's is a.

Community
  • 1
  • 1
PSC
  • 205
  • 1
  • 7