writedata - Maple Programming Help

Home : Support : Online Help : Programming : Input and Output : Output : writedata

writedata

write numerical data to a text file

 Calling Sequence writedata(fileID, data) writedata(fileID, data, format) writedata(fileID, data, format, default)

Parameters

 fileID - name or descriptor of a data file data - list or vector or list of lists or matrix over values format - specifies how the data is to be written default - procedure: how to write non-numerical data

Description

 • The writedata function writes numeric data from a Maple vector, matrix, list, or list of lists into a text file fileID.  Note, if the fileID = terminal the output is printed on the user's terminal.
 To write numerical data from a Matrix or Vector, see ExportMatrix and ExportVector.
 • The fileID is either the name of a file, or a file descriptor returned by the fopen function.
 • If data is a vector or list of values, each value is printed on a separate line.  If the data is a matrix or list of lists of values, the data is printed one row per line with values separated by tabs. Normally the data must consist of integers or floating-point numbers. Provision for handling non-numeric data and complex numeric data is provided - see below.
 • In the second form of the writedata function, the format option may be either the keyword integer, float (the default), or string. If integer, then numeric data is printed as integers. Rational and floating-point constants are truncated to the nearest integer. If float, then numeric data is written in floating-point format. Rational constants are first converted to floating point. If string, then strings are printed.
 • In the case of a matrix or list of lists of data, the format option may be a list of integer, float, or string which specifies how each column is to be printed.
 • The first form of the writedata function writes numerical data out in floating point format - that is, it is equivalent to the second form with format = float.
 • The default option allows you to specify a Maple procedure which prints a data value in your own format.  This provides a way to print out complex numeric data.  For example:

 writedata(F,A,float,proc(f,x) fprintf(f,CMPLX(%g,%g),Re(x),Im(x)) end proc);

 Sometimes it is useful to output formulae using the Maple one-dimensional notation.  This is available with the %a option in the fprintf function.  Hence:

 writedata(F,A,float,proc(f,x::algebraic) fprintf(f,%a,x) end proc);

 • The writedata function is intended to simplify the job of writing vectors and matrices of numerical data into a file. In addition to printing the data in the appropriate format, details such as opening the output file, turning off Maple diagnostics, resetting the output stream back to the user's terminal when an error occurs in the middle of the output, and the like are looked after by the writedata function. If you need to output data in a different format, you will need to program this yourself using fprintf function.  You may prefer to modify the code for the writedata function. You can print the code for the writedata function by doing:

 interface(verboseproc=2); print(writedata);

 • By default, writedata creates a new file every time it is called (unless the file is already open). If instead, writedata[APPEND] is called, the data is appended to the file if it already exists.
 • If the specified file is already open, writedata writes to the file at the current position within the file.
 • If a file name is given for the fileID (instead of a file descriptor), and the file is not already open, the file will be opened as a TEXT file in WRITE mode (or in APPEND mode if writedata[APPEND] was called). Furthermore, if a filename is given, the file will be closed when writedata returns.
 • The readdata function is the corresponding function for reading numerical data from a text file into Maple.  See readdata.

Examples

 > $A≔\mathrm{array}\left(\left[\left[1.5,2.2,3.4\right],\left[2.7,3.4,5.6\right],\left[1.8,3.1,6.7\right]\right]\right)$

 [ 1.5  2.2  3.4 ] [               ] A := [ 2.7  3.4  5.6 ] [               ] [ 1.8  3.1  6.7 ]

 > $\mathrm{writedata}\left(\mathrm{terminal},A,\mathrm{float}\right)$

 1.5    2.2    3.4 2.7    3.4    5.6 1.8    3.1    6.7

 > $\mathrm{writedata}\left(\mathrm{terminal},A,\mathrm{integer}\right)$

 1    2    3 2    3    5 1    3    6

 > $A≔\left[\left[x,1,3.4,2,5.3\right],\left[y,1,5.6,2,4.5\right]\right]$
 ${A}{:=}\left[\left[{x}{,}{1}{,}{3.4}{,}{2}{,}{5.3}\right]{,}\left[{y}{,}{1}{,}{5.6}{,}{2}{,}{4.5}\right]\right]$ (1)

The following is wrong because the strings x and y are not numeric

 > $\mathrm{writedata}\left(\mathrm{terminal},A,\mathrm{float}\right)$

 Error, (in writedata) Bad data found, x

 > $\mathrm{writedata}\left(\mathrm{terminal},A,\left[\mathrm{string},\mathrm{integer},\mathrm{float},\mathrm{integer},\mathrm{float}\right]\right)$

 x    1    3.4    2    5.3 y    1    5.6    2    4.5

 > $A≔\mathrm{array}\left(1..3,1..3\right):$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}j\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}_{i,j}≔\mathrm{evalf}\left({ⅇ}^{i+Ij},5\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$$\mathrm{print}\left(A\right)$

 [ 1.4687 + 2.2874 I   - 1.1312 + 2.4717 I   - 2.6911 + .38360 I ] [                                                               ] [ 3.9923 + 6.2177 I   - 3.0749 + 6.7188 I   - 7.3151 + 1.0427 I ] [                                                               ] [ 10.852 + 16.901 I   - 8.3585 + 18.264 I   - 19.885 + 2.8345 I ]

print the data using the Maple one-dimensional input format

 > writedata(terminal,A,integer,proc(f,x) fprintf(f,%a,x) end proc);

 1.4687+2.2874*I    -1.1312+2.4717*I    -2.6911+.38360*I 3.9923+6.2177*I    -3.0749+6.7188*I    -7.3151+1.0427*I 10.852+16.901*I    -8.3585+18.264*I    -19.885+2.8345*I

print the real and imaginary parts of the complex data in separate columns

 > P := proc(f,x::complex(numeric)) fprintf(f,%g\t%g,Re(x),Im(x)) end proc:
 > $\mathrm{writedata}\left(\mathrm{terminal},A,\mathrm{float},P\right)$

 1.4687    2.2874    -1.1312    2.4717    -2.6911    .3836 3.9923    6.2177    -3.0749    6.7188    -7.3151    1.0427 10.852    16.901    -8.3585    18.264    -19.885    2.8345

write two matrices to the same file

 > $\mathrm{H1}≔\mathrm{linalg}[\mathrm{hilbert}]\left(10\right):$
 > $\mathrm{H2}≔\mathrm{linalg}[\mathrm{hilbert}]\left(5\right):$
 > $\mathrm{fd}≔\mathrm{fopen}\left(\mathrm{testFile},\mathrm{WRITE},\mathrm{TEXT}\right):$
 > $\mathrm{writedata}\left(\mathrm{fd},\mathrm{H1}\right)$
 > $\mathrm{writedata}\left(\mathrm{fd},\mathrm{H2}\right)$
 > $\mathrm{fclose}\left(\mathrm{fd}\right)$