S-Lang function to create an ASCII output file from S-Lang arrays
writeascii( filename, col1 )
writeascii( filename, col1, col2 )
writeascii( filename, col1, col2, ..., colN )
Write the supplied S-lang arrays (i.e. "col1", "col2", ...)
to an ASCII file.
To write to stdout (i.e. the screen) or stderr use a filename of
"stdout" or "stderr" respectively, without the quotes.
Here we create two columns, and then write them to an ASCII file
called foo.dat.
chips> x = [1:10];
chips> y = sin(x) + 0.5 * x;
chips> writeascii("foo.dat",x,y);
As the output is an ASCII file, it can be accessed using
standard UNIX tools, such as cat:
unix% cat foo.dat
1 1.34147
2 1.9093
3 1.64112
4 1.2432
5 1.54108
6 2.72058
7 4.15699
8 4.98936
9 4.91212
10 4.45598
Instead of writing to a file, we can use writeascii()
to write to the screen (ie stdout).
This can also be achieved using the "printarr()" Varmm function,
although it is limited to printing out only one array.
This example also shows how you can access a field within
a S-Lang structure, here the second column read from the file "foo.dat".
chips> foo = readfile("foo.dat")
chips> writeascii(stdout,foo.col2)
1.34147
1.9093
1.64112
1.2432
1.54108
2.72058
4.15699
4.98936
4.91212
4.45598
The default format used to print out the column data may not match
your needs; for instance it may not display enough significant
figures. The format used can be changed by calling the
set_float_format() function from the S-Lang Run-Time Library.
In the following example we change the format to use the
format %13.6e, which means to use exponential notation with 6
numbers after the decimal point and placed within a space
13 characters wide.
chips> a = [0:10:2] / 3.0
chips> b = 10^a
chips> writeascii( stdout, a, b )
0 1
0.666667 4.64159
1.33333 21.5443
2 100
2.66667 464.159
3.33333 2154.43
chips> set_float_format( "%13.6e" )
chips> writeascii( stdout, a, b )
0.000000e+00 1.000000e+00
6.666667e-01 4.641589e+00
1.333333e+00 2.154435e+01
2.000000e+00 1.000000e+02
2.666667e+00 4.641589e+02
3.333333e+00 2.154435e+03
chips> set_float_format( "%g" )
Note that all the columns are printed using this format
and that we reset the format to the default value ("%g")
to avoid changing the layout of other commands,
such as print() and printarr().
|