WRITE [(FILE ("fileName" | fileNameStringVariable)) | WINDOW | ARGFILE] [MISSING literalNumber] [ROWS] [FILL] [HEADING] [APPEND] {inputVector [formatSpec]}

Writes the input vector(s) into a file or to the Statistics101 Output Window according to optional format specifications.

The keywords may be written in any order on the command line.

By default, each input vector becomes one column in the file or the Output Window. Alternately, if the rows keyword is used, then each input vector becomes one row in the file or Output Window.

  • The columns in the output will all be the length of the longest input vector. By default, any columns created from shorter vectors will be filled out with the appropriate delimiter but no element value. If the fill keyword is present, then the shorter columns will be filled out with NaN elements.

  • If the rows keyword is present, then by default each row in the file will have only as many elements as the vector that produced it. If the fill keyword is present, then each row in the file will be as long as the longest vector, with the rows representing shorter vectors filled out with NaNs.

If an input vector contains Named Constants, the names, not the values, of the constants will be written to the output file.

If the window keyword is present, the output of the WRITE command will be sent to the Statistics101 Output Window. You can use this to print tables to the Output Window. Or, if you want to write to a file, you can use the window keyword temporarily to test your formatSpecs by printing to the window, then when they are correct, you can change to the file keyword and write the results to a file.

If the file keyword and its filename argument are present, the output of the WRITE command will be sent to the designated file. The filename may be the complete path name or a relative path. A relative path will be relative to the current user directory (the directory containing the statistics101.jar file). If the file exists it will be overwritten unless the append keyword (see below) is also present.

If neither the file nor the window keyword is present, an "open file" dialog will appear and ask the user to name a new file or select an existing file to receive the output.

If both keywords are present in the same WRITE command, that is considered an error and will result in an error message.

The filename accompanying the file keyword must be enclosed in double quotes as shown in the examples. The file path names in Microsoft Windows systems must use either double backslash ("\\") or single forward slashes ("/") as delimiters between the names of nested directories. The reason for the double backslash is that in Java as in other C-derived languages, a backslash is interpreted as an escape character and two backslashes are interpreted as a single true backslash. This is true whether using Statistics101 in Windows, Unix, Linux, or MacOs. Users of non-Windows systems should use the forward slash.

If the argfile keyword is present, then the filename will be the path entered using the -w switch on the command line that invoked Statistics101. See Command Line Invocation. If the -w switch is absent or did not name a file, then the user will be requested to select a file.

The format specifications (formatSpec) describe the way that elements of each vector are to be written to the output file. Format specifications can use any of the format codes (f, e, g, b, or c) that are described below or no format codes. The general form of the WRITE (or PRINT) format specification is:

%width[.precision[code]]

where:

  • [...] means "optional" (0 or one occurrences).

  • % introduces the formatSpec, separating it from its related variable name.

  • width is a number that specifies the minimum width, in characters, that the number is allowed to have. The width includes the decimal point, any numbers before and after the decimal point, and the exponent, if any. For example, this number, 123.45, has width 6. This number, 1.234E02, has width 8. Note that the specified width is a minimum; therefore, if the number cannot be fit within the specified width, it will be allowed as much room as needed. If the width is greater than needed to fit the number, the number will be followed by spaces to fill the width.

  • precision is a number that specifies the maximum number of significant digits or the maximum number of decimal places of this specification's output. For example, this number, 123.45, has a precision of 2. The number 1.234E02 has a precision of 4. That's because the precision for a number in scientific notation refers to the number of significant digits. The precision for a fixed point number refers to the number of digits after the decimal point.

  • code is one of 'F', 'E', 'G', 'B', or 'C' either upper or lower case. If code is absent, it will be defaulted to "G".

    • F specifies fixed point output. The precision specifies the number of digits after the decimal point.

    • E specifies Scientific Notation output. precision specifies the number of significant digits.

    • G specifies "General" notation. This will be either fixed point or Scientific Notation depending on the number.

    • B is same as G, but a blank is appended following the output number.

    • C is the same as G except that a comma is appended following the output number.

For the E, F, and G codes, the delimiter between output columns is a single tab. The codes may be entered in either upper or lower case. No distinction is made by Statistics101.

If there is more than one input vector, you should use the same delimiter (comma, tab, or blank) for all vectors. It will work with different delimiters, but the results might not be what you want. e.g., when reading the file back in, the varying delimiters might cause misalignment of the data with the vectors.

If there is more than one input vector and you use a format spec for some of the vectors and no format spec for one or more of the vectors, the vectors without format specs will be formatted with the default spec "%14.6G".

If you use no format specifications, as in the first two examples at the right, the result will be that all the values will be written with maximum precision, will be separated by commas, and will have no space or tab characters. This is the format of a standard CSV (Comma Separated Values) file.

The keyword missing allows you to specify a number that represents missing data. Whenever that number is encountered during the writing of the data to the file, "NaN" will be substituted for it.

The keyword rows forces the WRITE command to write each argument vector to the file or window as a row instead of a column.

The keyword append instructs the WRITE command to add the specified data to the end of a file if the file already exists. If you rerun a program that writes using append, the data generated by the latest run will be appended to the data from the previous run(s). If you want each run to be independent, you can delete or rename the file manually between runs. Or you can have your program automatically delete it at the start by writing to the file with a WRITE command that lacks the append keyword and has no vector arguments. This will overwrite the existing file with an empty file. Then in the main part of your program you can use the WRITE command with the append keyword to append your data.

If the append keyword is not present and if the file already exists, the existing file will be overwritten by the new data.

If the append keyword is used in a WRITE command that also has the window keyword, the append keyword is ignored.

The keyword heading forces the WRITE command to output the names of the vectors that are its arguments, in addition to the vector elements of the arguments. If the rows keyword is absent, the vector names will be output to the first line of the output file, separated by commas or by the separator that is required by that vector's format spec. If the rows keyword is present, the name of each vector will be the first item in that vector's row of the output file. The READ command also has the heading keyword so it can properly input a file generated by WRITE.

See also: OUTPUT, PRINT, READ.

These first two examples show the two ways (double backslash and single forward slash) of delimiting file paths. The double backslash is for Windows. The single forward slash is preferred because it works in any operating system, including Windows. Also, since the input vector names are not associated with format codes, the output files will be CSV files. The files are named with a ".csv" extension although you could use ".txt" or any other, or even no extension if that is appropriate to your purpose. This is the easiest and probably the most common way to use the WRITE command.

WRITE file "C:\\folder1\\folder2\\file.csv" a b c

Or

WRITE file "C:/folder1/folder2/file.csv" a b c

Other examples: The first uses the append keyword. This causes the data to be written to the end of the file if it already exists. The second doesn't specify a file name, so when that WRITE command is executed, it will open a dialog box to allow the user to select or enter the desired file name.

WRITE file "C:/folder1/folder2/file.txt" append a%12.8G b%3.6F c%3.6F

WRITE a%12.8G b%3.6F c%3.6F

This program:

COPY 1,10 numbers
SQRT numbers roots
OUTPUT "Number  Sq. Root\n"  'Column titles
WRITE window numbers%4.2F roots%3.6F

produces the following table in the Statistics101 Output Window:

Number  Sq. Root
1       1  	
2       1.414214	
3       1.732051	
4       2  	
5       2.236068	
6       2.44949	
7       2.645751	
8       2.828427	
9       3  	
10      3.162278	

Here are two simple examples to show the results of the heading keyword. These write to the Output Window for simplicity, but their output to a file would be exactly the same.

COPY 1,10 var1
COPY 11,20 var2
COPY 21,30 var3
WRITE window heading var1 var2 var3

Result:

var1,var2,var3
1.0,11.0,21.0
2.0,12.0,22.0
3.0,13.0,23.0
4.0,14.0,24.0
5.0,15.0,25.0
6.0,16.0,26.0
7.0,17.0,27.0
8.0,18.0,28.0
9.0,19.0,29.0
10.0,20.0,30.0

Compare with:

COPY 1,10 var1
COPY 11,20 var2
COPY 21,30 var3
WRITE window rows heading var1 var2 var3

Result:

var1,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0
var2,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0
var3,21.0,22.0,23.0,24.0,25.0,26.0,27.0,28.0,29.0,30.0