MathGroup Archive 2002

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

Search the Archive

Re: Use of Import inside of Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33321] Re: [mg33299] Use of Import inside of Module
  • From: WhoamI <lushstring at yahoo.com>
  • Date: Thu, 14 Mar 2002 19:51:23 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

--- "Philip M. Howe" <pmhowe at lanl.gov> wrote:
> 
> 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.

Hi Philip, the problem in your code is the argument
you are passing to the "g" function...Try this
 
 Clear[g, ndat, mdat, file]; 
 g[file_] := 
   Module[{ndat, mdat}, ndat = Import[
               ToString[file], "Table"]; 
     mdat = Transpose[{Transpose[ndat][[1]], 
                      Transpose[ndat][[2]]}]; 
     ListPlot[mdat, PlotJoined -> True]]

 g[myfile] 

or better:

 Clear[g, ndat, mdat, file]; 
 g[file_String] := 
   Module[{ndat, mdat}, ndat = Import[file, "Table"]; 
     mdat = Transpose[{Transpose[ndat][[1]], 
                       Transpose[ndat][[2]]}]; 
     ListPlot[mdat, PlotJoined -> True]]

 g["myfile"]
 
The last is better because the name of the file can
contain an extension for example "myfile.dat". The
first approach can't do it:

 In[1]:= Characters[ToString[myfile.dat]]
 Out[1]= {m, y, f, i, l, e,  , .,  , d, a, t}

because of the blancks which appear because of the dot
product.

Greetings...
Cesar Guerra
Secc. Fisica - PUCP
Lima - Peru

 

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/


  • Prev by Date: Re: Looking for Review on Analog Insydes
  • Next by Date: Re: Use of Import inside of Module
  • Previous by thread: Re: Use of Import inside of Module
  • Next by thread: Re: Use of Import inside of Module