next up previous contents index
Next: Substitution and Command line Up: Gnuplot Previous: Start-up   Contents   Index


String constants and string variables

In addition to string constants, most gnuplot commands also accept a string variable, a string expression, or a function that returns a string. For example, the following four methods of creating a plot all result in the same plot title:


     four = "4"
     graph4 = "Title for plot #4"
     graph(n) = sprintf("Title for plot #%d",n)


     plot 'data.4' title "Title for plot #4"
     plot 'data.4' title graph4
     plot 'data.4' title "Title for plot #".four
     plot 'data.4' title graph(4)

Since integers are promoted to strings when operated on by the string concatenation operator, the following method also works:


     N = 4
     plot 'data.'.N title "Title for plot #".N

In general, elements on the command line will only be evaluated as possible string variables if they are not otherwise recognizable as part of the normal gnuplot syntax. So the following sequence of commands is legal, although probably should be avoided so as not to cause confusion:


     plot = "my_datafile.dat"
     title = "My Title"
     plot plot title title

There are three binary operators that require string operands: the string concatenation operator ".", the string equality operator "eq" and the string inequality operator "ne". The following example will print TRUE.


    if ("A"."B" eq "AB") print "TRUE"

See also the two string formatting functions gprintf (p. [*]) and sprintf (p. [*]).

Substrings can be specified by appending a range specifier to any string, string variable, or string-valued function. The range specifier has the form [begin:end], where begin is the index of the first character of the substring and end is the index of the last character of the substring. The first character has index 1. The begin or end fields may be empty, or contain '*', to indicate the true start or end of the original string. E.g. str[:] and str[*:*] both describe the full string str.


next up previous contents index
Next: Substitution and Command line Up: Gnuplot Previous: Start-up   Contents   Index
Ethan Merritt 2007-03-03