Re: Sending data to a file
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Sending data to a file
- From: twj
- Date: Mon, 26 Apr 93 14:17:28 CDT
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
Here are two functions which will read and write files of this
form...
ImportNumberData[ file_String] :=
ReadList[ file, Number, RecordLists -> True]
ExportNumberData[ file_String,
data_List /; Apply[ And, Map[ VectorQ[#, NumberQ]&,data]]] :=
Block[{stm, i, j},
stm = OpenWrite[ file] ;
If[ Head[ stm] =!= OutputStream, Return[ $Failed]] ;
Do[
Do[
WriteString[ stm, ToString[ Part[ data, i, j]], "\t"],
{j, Length[ Part[ data, i]]-1}];
WriteString[ stm, ToString[ Part[ data, i, -1]], "\n"],
{i, Length[ data]}] ;
Close[ stm]
]
You can use them so..
data = Table[ i j, {i, 5}, {j, 1, 2,.5}]
ExportNumberData[ "file.dat", data]
ImportNumberData[ "file.dat"]
Tom Wickham-Jones
WRI