Is there a way to convert strings representing numbers (in scientific format) to numbers in gnuplot. I.e.
stringnumber="1.0e0"
number=myconvert(stringnumber)
plot [-1:1] number
I search myconvert possibly using shell commands.
Another simple approach (probably the gnuplot intended way), which hasn't been mentioned here yet: I guess "since ever" gnuplot offers the functions int() and real(), (check help int and help real).
If you know your string is an integer number do: int(<string>) and if you are not sure and it could be a floating point number use real(<string>).
Check the following example:
Script:
### convert strings to numbers
reset session
$Data <<EOD
1
-12
1.2
-123.4
1.23e-4
-123.4e+56
EOD
do for [i=1:|$Data|] {
s = $Data[i] # s is a string
print real(s)
}
### end of script
Result:
1.0
-12.0
1.2
-123.4
0.000123
-1.234e+58
Addition:
Let's go one step further. gnuplot can handle complex numbers.
Typically, they are written in the form: x ± i*y or x ± j*y, where i and j are the imaginary unit i = j = sqrt(-1).
But how do I import complex numbers in this format from a text file into gnuplot?
In gnuplot, such complex numbers are represented as {x, y} (note the curly brackets, check help complex).
Unfortunately, gnuplot doesn't support regular expressions, but you can build your own function to mimic it. Here is an attempt:
(Actually, I just learnt that x ± y*i is another used representation which would require a few extra lines for parsing (e.g. -1.2e-3-4.5e-6i), but I guess it can be done as well).
Script:
### convert complex strings to numbers
reset session
$Complex <<EOD
123
i1.2
-j12.3
12 j0
123+j456
1.2+i3.4
1.2e-3-i4.5e-6
EOD
j = {0,1} # imaginary unit
cmplxStr2number(s) = \
(_c='', (sum [_k=1:6] (_c0=word("i j +i +j -i -j",_k), \
strstrt(s,_c0) ? _c=_c0 : 0, 0)), !strlen(_c) ? (_c1=real(s), _c2=0) : \
(_c3=strstrt(s,_c), !strlen(word(s[1:_c3-1],1)) ? _c1=0 : _c1=real(s[1:_c3-1]), \
_c2=(_c[1:1] eq "-" ? -1 : 1)*real(s[_c3+strlen(_c):])), _c1 + j*_c2 )
do for [i=1:|$Data|] {
s = $Complex[i] # s is a string
print cmplxStr2number(s)
}
### end of script
Result:
123.0
{0.0, 1.2}
{0.0, -12.3}
12.0
{123.0, 456.0}
{1.2, 3.4}
{0.0012, -4.5e-06}