MathGroup Archive 2007

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

Search the Archive

Re: Block[], OpenRead[], & /@

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75911] Re: Block[], OpenRead[], & /@
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 13 May 2007 05:46:22 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f23pbj$nh8$1@smc.vnet.net>

Mary Beth Mulcahy wrote:
> I am trying to write a more succint code to import multiple data files, but
> I am having problems mapping the Block[] command with an OpenRead[]
> command.  As my code is now, I read the files names from the parent
> directory and then cut and past all the names into the OpenRead[] command
> seen below.  In the code, I give each set of data a different name.
> 
> 
> files = FileNames["*txt"]
> 
> strm6 = OpenRead["Au6002.txt"]
> Skip[strm6, Record, 4, NullRecords -> False]       (*First 4 lines of each
> files contains non-numeric values, so skip these lines*)
> six= ReadList[strm6, Table[Number, {i, 1, 19}]]     (*Put the data into a
> table called by a name*)
> Close[strm6]
> 
> 
> I would like to take automat this so that I am not copying and pasting file
> names so I thought I could use the Block[] funtion and then map the process
> onto all the file names in the directory.  When I imput the following code:
> 
> Block[{str}, str = OpenRead[#]; Skip[str, Record, 4,
>    NullRecords -> False]; ReadList[
>     str, Table[Number, {i, 1, 19}]]; Close[str]] & /@ files
> 
> 
> my output is:
> {Au6001.txt, Au6002.txt, Au6003.txt, Au6004.txt, Au6005.txt}
> 
> I do not understand why I get the output I see above.  When I modify the
> code above to:
> 
> 
> Block[{str}, str = OpenRead[#];
> Skip[str, Record, 4,
>       NullRecords -> False]; one = ReadList[
>       str, Table[Number, {i, 1, 19}]]; Close[str]] & /@ files
> 
> and ask for
> 
> one // TableForm
> 
> then I get the data for the last file in the list of file names which
> indicates to me that I may be close in getting this whole thing to work.
> 
> 
Hi Mary Beth,

You should use Scan rather than Map, and a combo of Sow and Reap to 
construct your list of values.

Reap[Scan[Block[{str, lst}, str = OpenRead[#1];
       Skip[str, Record, 4, NullRecords -> False];
       lst = ReadList[str, Table[Number, {i, 1, 19}]];
       Sow[lst]; Close[str]] & , files]]

Best Regards,
Jean-Marc


  • Prev by Date: Re: Re: axes labeling
  • Next by Date: Re: elliptic integral (reloaded!)
  • Previous by thread: Re: Block[], OpenRead[], & /@
  • Next by thread: Re: Block[], OpenRead[], & /@