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: [mg33308] Re: Use of Import inside of Module
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 14 Mar 2002 19:51:07 -0500 (EST)
  • References: <a6pjo7$apn$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Philip,
Looking at your code

> 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];

The problem is nothing to do with Module, it is that the symbol  file in
file_ is not the same as the string "file" in Import["file", "Table"], so
nothing is passed to the right when you evaluate g[myfile], consequently
Import["file", "Table"] is evaluated.

Two ways out ( we cannot use "file"_ )

If you want to use g[myfile] the use Import [ToString[file],"Table"].
If you want to use g["myfile"] then use Import[file,"Table"]

One more thing

mdat = ndat[[All,{1,2}]]

is both neater and quicker than

mdat = Transpose[{Transpose[ndat][[1]], Transpose[ndat][[2]]}];

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Philip M. Howe" <pmhowe at lanl.gov> wrote in message
news:a6pjo7$apn$1 at smc.vnet.net...
>
> 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
>




  • Prev by Date: Re: Use of Import inside of Module
  • Next by Date: Mathlink for Excel
  • Previous by thread: Re: Use of Import inside of Module
  • Next by thread: webMathematica