Re: help: Write data to file into many columns
- To: mathgroup at smc.vnet.net
- Subject: [mg4450] Re: help: Write data to file into many columns
- From: pecora at zoltar.nrl.navy.mil (Louis M. Pecora)
- Date: Mon, 29 Jul 1996 02:37:10 -0400
- Organization: Naval Research Laboratory
- Sender: owner-wri-mathgroup at wolfram.com
In article <4shucf$g7n at ralph.vnet.net>, bp at ee.edinburgh.ac.uk (Bumroong
Puangkird ) wrote:
> I am trying to write my data into a file by using Mathematica. I need to
> write my data into two or three column formats. However, I could not do
> it so. Any help is appreciated.
Well, I won't guarantee these are the best way to do it, but these work
for me and should be adaptable to your case. Functions ending in LP are
the actual file-writing funcitons, the others are "front ends." There are
routines for space or tab delimited writing of 2D arrays ("matrices") and
a separate function for just writing a list. Hope this helps.
(* Write out to file with spaces after numbers -- does actual writing *)
arrayoutLP[thefile_,array_] := (
Scan[(
WriteString[thefile,#];
WriteString[thefile," "])&,
array ];
WriteString[thefile,"\n"]; )
arrayWriteLP[thefile_,array_] := Module[{d,i},
d=Depth[array];
If[d < 2,
Print["***ERROR. Input array has no elements to output (Depth < 2)"] ];
If[d == 2,
arrayoutLP[thefile,array] ,
Do[
arrayWriteLP[thefile,array[[i]] ],
{i,Length[array]}]; ]
]
(* Array and List I/O to space-delimited CR at line end files -- Front end *)
(* -------- arrayWrite ----------------------------------
Output numbers from array in text format with spaces between each list
element and a CR after each list *)
arrayWrite[filename_,array_] := Module [{thefile=OpenWrite[filename]},
If[thefile==$Failed,
Print["***ERROR. Could not open file"];
Return[Null]; ];
arrayWriteLP[thefile,array];
Close[thefile];
Return[Null]
]
(* Write out to file with tabs after numbers -- does actual writing *)
arrayouttabLP[thefile_,array_] := (
WriteString[thefile,array[[1]]]; (* write first number *)
Scan[(
WriteString[thefile,"\t"];
WriteString[thefile,#];)&, (* write rest of numbers *)
Rest[array] ];
WriteString[thefile,"\n"]; )
arrayWritetabLP[thefile_,array_] := Module[{d,i},
d=Depth[array];
If[d < 2,
Print["***ERROR. Input array has no elements to output (Depth < 2)"] ];
If[d == 2,
arrayouttabLP[thefile,array] ,
Do[
arrayWritetabLP[thefile,array[[i]] ],
{i,Length[array]}]; ]
]
(* Array and List I/O to space-delimited CR at line end files *)
(* -------- arrayWrite ----------------------------------
Output numbers from array in text format with spaces between each list
element and a CR after each list *)
arrayWritetab[filename_,array_] := Module [{thefile=OpenWrite[filename]},
If[thefile==$Failed,
Print["***ERROR. Could not open file"];
Return[Null]; ];
arrayWritetabLP[thefile,array];
Close[thefile];
Return[Null]
]
(* -------- listWrite -----------------------------------------
Output numbers from list in text format with CR after each *)
listWrite[filename_,list_] := Module [{thefile=OpenWrite[filename],d},
If[thefile==$Failed,
Print["***ERROR. Could not open file"];
Return[Null]; ];
d=Depth[list];
If [d != 2,
Print["***ERROR. Input list has wrong depth. Can only be a single list"];
Return[Null] ];
Scan[(
WriteString[thefile,#];
WriteString[thefile,"\n"])&,
list ];
Close[thefile];
]
--
Louis M. Pecora
pecora at zoltar.nrl.navy.mil
/* My views and opinions are not those of the U.S. Navy.
If you want those, you have to start a war. */
==== [MESSAGE SEPARATOR] ====