Re: Use of Import inside of Module
- To: mathgroup at smc.vnet.net
- Subject: [mg33314] Re: [mg33299] Use of Import inside of Module
- From: BobHanlon at aol.com
- Date: Thu, 14 Mar 2002 19:51:15 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 3/14/02 4:27:20 AM, pmhowe at lanl.gov writes:
>... 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?
"file" is the literal string file and is not replaced by your file name.
Use ToString
Clear[g];
g[file_]:=
Module[{ndat,mdat},
ndat=Import[ToString[file],"Table"];
mdat=Take[#,2]& /@ ndat;
ListPlot[mdat,PlotJoined->True]]
;
g[myfile];
Or have the input be a string
Clear[g];
g[file_String]:=
Module[{ndat,mdat},
ndat=Import[file,"Table"];
mdat=Take[#,2]& /@ ndat;
ListPlot[mdat,PlotJoined->True]]
;
g["myfile"];
Bob Hanlon
Chantilly, VA USA