re: Sending data to a file

*To*: mathgroup at yoda.physics.unc.edu*Subject*: re: Sending data to a file*From*: myers at helios.tn.cornell.edu*Date*: Thu, 29 Apr 93 11:27:56 EDT

David Deutsch writes: > Suppose I create a list of data points, for example: > >data = {{1,2},{2,5},{3,-2},{4,7}} > > >How can I write this data to file so that in the file it has the >following form: > > 1 2 > 2 5 > 3 -2 > 4 7 To some extent, the answer to this question depends on what kinds of numbers are actually contained in the list and what one wants to do with them once they are written out to a file. I typically generate real numbers of all sizes and wish to write them out to a file so that they can be read in and plotted by something other than Mathematica. The solution proposed by Roland Jakschewitz, namely, OutputForm[ TableForm[ data, TableAlignments->{Top,Right}, TableSpacing->{0,1} ] ] >> "dataFile" will not work properly since large and small numbers (those with an exponent greater than +4 or less than -4, I think) get written out in a two-line raised power notation, which is useless if the numbers are to be read in by another program. I have written a function XYForm[] (which gets run typically as "XYForm[data] >> file") that fixes this. The function is more general than that requested, in that it writes out lists of any dimension. The dimension = 2 case mentioned above goes through as usual; the dimension = 3 case takes a list of x-y data sets (such as that specified above) and writes out each x-y pair as desired while inserting a blank line between sets. I chose this format in part for compatibility with xvgr/xmgr, a public domain x-y plotting program that's rather nice. The second optional argument specifies a spacing between columns which defaults at 6. XYForm[x_, col_:6] := ( Switch[Length[Dimensions[x]], 0, CForm[x], 1, OutputForm[TableForm[Map[CForm, x, {1}], TableSpacing -> {0}]], 2, OutputForm[TableForm[Map[CForm, x, {2}], TableSpacing -> {0,col}]], _, OutputForm[TableForm[Map[CForm,Transpose[x, {1,3,2}], {3}], TableSpacing -> {1,col,0,col}]] ] ) Chris Myers myers at helios.tn.cornell.edu