Use of Import inside of Module
- To: mathgroup at smc.vnet.net
- Subject: [mg33299] Use of Import inside of Module
- From: "Philip M. Howe" <pmhowe at lanl.gov>
- Date: Thu, 14 Mar 2002 02:22:01 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi All,
I have some fairly large data files that I am working with in
Mathematica. Each data file contains five columns of data, the first
two of which are of interest to me, so I perform a few manipulations
to convert the first two columns into a list of pairwise data, which
I then plot. I have generated a toy file ("myfile"), to illustrate
the point - myfile is a text file containing the following:
0 0 1 2 3
1 0 2 3 5
2 0 2 2 4
3 0 3 3 4
4 1 4 4 5
5 .9 4 5 6
6 .8 4 5 6
7 .6 4 5 6
8 .5 4 6 6
9 .4 5 5 6
10 .4 5 5 6
This small file could easily be pasted, but the large files are
unwieldy, so it is nice to use "Import". Using "Import", the
following does what I want:
ndat = Import["myfile", "Table"];
mdat = Transpose[{Transpose[ndat][[1]], Transpose[ndat][[2]]}];
ListPlot[mdat, PlotJoined -> True];
Since I have numerous files to work with, I wish to combine the above
into a single function, which contains all the steps. It looks like
the snippet below ought to work, but it doesn't:
Clear[g, ndat, mdat, file];
g[file_] := Module[{ndat, mdat},
ndat = Import["file", "Table"];
mdat = Transpose[{Transpose[ndat][[1]], Transpose[ndat][[2]]}];
ListPlot[mdat, PlotJoined -> True]]
g[myfile];
Apparently, the problem lies with embedding Import within Module. I
can generate a work-around:
ndat = Import["myfile", "Table"];
g[file_] := Module[{mdat},
mdat = Transpose[{Transpose[file][[1]], Transpose[file][[2]]}];
ListPlot[mdat, PlotJoined -> True]];
g[ndat];
However, using Module might be neater, and I would like to know what
I am doing wrong, anyway. Any suggestions?
Thanks for the help.
P.
--
Philip M. Howe
Program Manager, Stockpile Surety
Los Alamos National Laboratory
(505) 665-5332
(505) 667-9498
Fax: 505-665-5249
email pmhowe at lanl.gov
Mail Stop P945