Next: Gnuplot-defined variables
Up: Operators
Previous: Binary
Contents
Index
Ternary
There is a single ternary operator:
Ternary Operator |
Symbol |
Example |
Explanation |
?: |
a?b:c |
ternary operation |
The ternary operator behaves as it does in C. The first argument (a), which
must be an integer, is evaluated. If it is true (non-zero), the second
argument (b) is evaluated and returned; otherwise the third argument (c) is
evaluated and returned.
The ternary operator is very useful both in constructing piecewise functions
and in plotting points only when certain conditions are met.
Examples:
Plot a function that is to equal sin(x) for 0 5#5= x 5#5 1, 1/x for 1 5#5= x 5#5 2,
and undefined elsewhere:
f(x) = 0<=x && x<1 ? sin(x) : 1<=x && x<2 ? 1/x : 1/0
plot f(x)
Note that gnuplot quietly ignores undefined values, so the final branch of
the function (1/0) will produce no plottable points. Note also that f(x)
will be plotted as a continuous function across the discontinuity if a line
style is used. To plot it discontinuously, create separate functions for the
two pieces. (Parametric functions are also useful for this purpose.)
For data in a file, plot the average of the data in columns 2 and 3 against
the datum in column 1, but only if the datum in column 4 is non-negative:
plot 'file' using 1:( $4<0 ? 1/0 : ($2+$3)/2 )
Please see plot datafile using (p. ) for an explanation of the using (p. ) syntax.
Next: Gnuplot-defined variables
Up: Operators
Previous: Binary
Contents
Index
Ethan Merritt
2007-03-03