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 <= x < 1, 1/x for 1 <= x < 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 it is plotted as a continuous function across the discontinuity if a line style is used. If you want it to be plotted discontinuously, create separate functions for the two pieces.

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 data-file using for an explanation of the using syntax.