Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: 2D array to tab delimited file

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24738] Re: 2D array to tab delimited file
  • From: "Louis M. Pecora" <pecora at anvil.nrl.navy.mil>
  • Date: Wed, 9 Aug 2000 02:31:40 -0400 (EDT)
  • Posted-and-mailed: yes
  • References: <8mdlkm$5in@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article <8mdlkm$5in at smc.vnet.net>, L Lobo <leonlobo at hotmail.com>
wrote:

> I am creating a large 2D array and need to save it to file (tab delimited)
> which is then read by LabVIEW. I have tried using Export but the spaces
> created between columns varies. I have looked up old postings on this
> newsgroup but the solutions I've seen seem to be a bit longwinded. I have
> version 4. Any ideas?

Here are some routines I wrote to output the type of files you want. 
Hope these help:

(* 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 *)

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]  ]
                        

(* Array and List I/O to tab-delimited CR at line end files *)

(* -------- arrayWrite ----------------------------------
   Output numbers from array in text format with tabs 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"];
      Close[thefile];
      Return[Null] ];
   Scan[(
      WriteString[thefile,#];
      WriteString[thefile,"\n"])&,
      list ];
   Close[thefile];  ]

--- Low level routines ----------------------------------
(*  Write out to file with spaces after numbers *)
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]}]; ] ]
   

(*  Write out to file with tabs after numbers *)
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]}]; ] ]


  • Prev by Date: RE: Package naming
  • Next by Date: RE: Test for On/Off
  • Previous by thread: 2D array to tab delimited file
  • Next by thread: Help Visualization Advice ?