Re: Importing (and dealing with) multiple files
- To: mathgroup at smc.vnet.net
- Subject: [mg60827] Re: Importing (and dealing with) multiple files
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Thu, 29 Sep 2005 05:42:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 9/28/05 at 1:41 AM, contact at dantimatter.com wrote:
>I have experimental data that is stored in a number of files. I've
>been importing the files (as tables) into Mathematica one-by-one,
>and working on them like so:
>rawdata=Import["C:\\filename.ASC", "Table"];
>croppedcorrelation=Take[rawdata,{75,185}];
>statistics=NonlinearRegress[croppedcorrelation, math_here...]
>then painstakingly picking out the relevant statistical parameter
>from each file, collecting them all, and later plotting them. What
>I'd really like to do is import an entire directory's worth of
>these files (each file is a different time point), do the same
>statistical thing to each file, save the relevant parameters from
>each file in a table form and then plot the whole shebang.
>One of the problems is that I don't know ahead of time how many
>.ASC files are in the directory, so I need to set up the For loop
>(or whatever structure) to account for that. I tried using
>FileNames[] to get the list of files, assigning them to an array,
>then using Import[] on each of those array elements. It didn't
>work.
>Does anyone have any suggestions?
Yes, use Mathematica's functional programming paradigm. Your specific example could be accomplished as follows.
First create a function to read the data and return the information you want, i.e.,
regressStatistics[filename_String]:=
Module[{rawdata=Import[filename, "Table"},
NonlinearRegress[Take[rawdata, {75,185}, math_here...]]
then Map this function to the list of files returned by FileNames after using SetDirectory to set the working directory to whatever directory contained the files, e.g.,
SetDirectory[directoryname];
regressStatistics[#]&/@FileNames@"*ASC"
that would result in a list containing the results for each .ASC file.
Probably better would be to write the results out to individual files doing something like:
Export[StringDrop[#,-3]<>"txt", regressStatistics[#], "Table"]&/@FileNames"*ASC"
Do note, I would not write the regressStatistics along the lines I've shown here. I simply tried to use syntax that adhered closely to the syntax in your original post. My experience is it is seldom a good idea to hard code indexes inside a function. If what I needed to do was complex enough to warrent a function, I would make the indices arguments to the function rather than hard coding specific values inside the function.
--
To reply via email subtract one hundred and four