MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

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









  • Prev by Date: MemoryInUse[] reports wrong numbers?
  • Next by Date: Re: Replacement?
  • Previous by thread: MemoryInUse[] reports wrong numbers?
  • Next by thread: re: Sending data to a file