15.3 Output Format Strings


x = 34912 string (format, v1 {, v2, ...})
converts the vector v1 (or all input vectors) to a string using the format string format
34915 write (x, "file.dat" {,mode {,format}})
writes text or numeric arrays to the file file.dat; optionally a mode mode and a format string format can be given

The use of format strings in XploRe 's 34922 string command comes close to the usage of C format strings. 34925 string is mainly used to convert numeric vectors to strings in order to append them to string objects.

Let us consider some examples which can also be found in 34928 XLGiofmt03.xpl . The following code line shows how to format all elements of a vector in the same way:

fmt = "Number  : %10.3f"
i   = 1:3
str = string (fmt, i)
str
gives
  Contents of str
  [ 1,] "Number  :      1.000"
  [ 2,] "Number  :      2.000"
  [ 3,] "Number  :      3.000"
Here we assigned a total of 10 characters to the float number. Three of these characters are reserved for digits after the ".".

The following example shows how different format strings effect the output format of pi (the constant $ \pi$):

string ("%8.5g", pi)
string ("%1.0f", pi)
string ("%22.20f", pi)
shows
  Contents of string
  [1,] "3.1416
  Contents of string
  [1,] "3"
  Contents of string
  [1,] "3.14159265358979311600"
The first string is the default representation when a float number is converted into a string. (One can check this default by issuing getenv("outputformat"), see Section 15.4.) The second string shows pi rounded to an integer, since 0 decimals are required. In the third string, 22 digits are converted into a string with 20 of them reserved for decimals.

Finally, let us consider the differences between the f-, e- and g-formats, which produce the usual decimal notation, exponential notation or a combination of both, respectively. The following example makes use of the possibility to use more than one format in the format string.

string ("%10.5f and %10.5g and %10.5e", pi, pi, pi)
will print
  Contents of string
  [1,] "   3.14159 and     3.1416 and 3.14159e+00"
in the output window. The first format requires a string of length 10, with exactly 5 decimals. The second format is used to print pi with a precision of 5 digits, as before the string length is set to 10. In comparison with the f-format, the g-format has the advantage that it would automatically use the exponential representation if the string length -- here 10 -- is exceeded. The third format forces the exponential representation, with a precision of 5 decimals. The string length 10 is not useful here and hence ignored.

As we have seen in the previous section, the function 34931 write stores data to a data file. 34934 write can save both numeric or text data. The 34937 write function has two optional parameters, the writing mode and the format string.

The writing mode can be set to "a" if the data to be written should be appended to an existing file. If the writing mode is different from "a", the file will be reset.

For numeric data, a format string can be given as an optional parameter, which will save the data in the specified format. For example,

x=(1:4)~(2:5)
write(x,"mydata.dat","r","%2.0f")
writes the matrix to the data file mydata.dat using the format "%2.0f". This file has then the following contents:
  1  2
  2  3
  3  4
  4  5