Re: Block[], OpenRead[], & /@
- To: mathgroup at smc.vnet.net
- Subject: [mg75913] Re: Block[], OpenRead[], & /@
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Sun, 13 May 2007 05:47:23 -0400 (EDT)
- References: <f23pbj$nh8$1@smc.vnet.net>
Hi,
and *what* has your question to do with Block[] or OpenRead[]
when your code
Block[{str}, str = OpenRead[#];
Skip[str, Record, 4,
NullRecords -> False]; one = ReadList[
str, Table[Number, {i, 1, 19}]]; Close[str]] & /@ files
read the contents of the file into the variable "one" and every
execution of the code overwrite the old value of "one" ..
You have to write
Block[{str,one}, str = OpenRead[#];
Skip[str, Record, 4,
NullRecords -> False]; one = ReadList[
str, Table[Number, {i, 1, 19}]]; Close[str];one] & /@ files
to return the actual value of one to Mathematica
Regards
Jens
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.
>
>