MathGroup Archive 1996

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

Search the Archive

Re: Re: Reading tables with labels..with an arbitrary number of columns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5586] Re: [mg5544] Re: Reading tables with labels..with an arbitrary number of columns
  • From: Joel Cannon <cannon at alpha.centenary.edu>
  • Date: Fri, 27 Dec 1996 01:59:00 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

> 
> William R. Pearson wrote:
> > 
> > I would like to read a table that looks like this:
> > 
> > name        len lamr   lamv   kr     kv
> > FEPE         54 0.2316 0.1544 0.4582 0.2030
> > H3NJ1W       60 0.2136 0.1826 0.1589 0.1244
> > 
> > Into two lists, one with the name of the column and a second with
> > the actual values of the columns.  I can read the data with ReadList[],
> > but of course it chokes on the initial labels.
> 
> 
> 	s = OpenRead["yourdatafile"];
>  s2 = StringToStream[Read[s, String]];
>  labels = ReadList[s2, Word];
>  data = ReadList[s, your_format_specification];
> 
> StringToStream creates a stream from a string, which means you can then
> use ordinary Read/ReadList operations on it.  It is analogous to
> using sscanf (as opposed to scanf) in C.
> 
> Has anybody got a better way to do this, in situations where you don't
> know the number of labels in advance?

This is not necessarily better but works for what I do. 

Below is a set of functions that reads files with columns of data with
a label at the head of each column. It stores the data in the global
(sorry!) list "d", and the data column label in the global list
"vartitles". To read a file of data

  It assumes that the first line of the file is a title header,
and the second line contains the column headings. It determines the
number of variables (and the size of each column) from the first line
of data (3rd line of the file). It opens the file twice: once to
determine the format and get the titles and once to get the data.

 It violates sound programming by using global variables and probably
through many other sins as well.  I post it here despite the
embarassment because it might still be useful to people who need to
read a variety of column-oriented data files.  It should be easy to
modify to read labels in the first column by changing the readlist as
per Dave Wagner's instructions, and should be easy to modify to
eliminate the global variables or fit someone elses data format.

To use it, load the following functions into your mathematica session,
and type:

reader["datafilename"]

and your data will be stored in list "d" and variable titles in list
"vartitles". 

Hope someone finds this useful.

*******************************
(* Start of functions to read column-oriented data with first line a
file title and description (tossed), the second the labels of the
various columns, followed by data. *)

reader[name_String] := 
  Block[{colstring, astring, bstring}, 
   readtitles[name]; title = astring; reader[name, numvars]]
 


reader[name_String, numcol_] := 
  (f = OpenRead[name]; Read[f, String]; Read[f, String]; 
    d = ReadList[f, t = Table[Number, {numcol}]]; Close[name])
 
 
readtitles[name_String] := 
  (Clear[bstring]; Clear[astring]; Clear[colstring]; f = OpenRead[name]; 
    astring = Read[f, String]; bstring = Read[f, String]; 
    colstring = Read[f, String]; colstring = Read[f, String]; Close[name]; 
    vartitles = getvarnames[colstring, bstring]; numvars = Length[vartitles])
 
 
getvarnames[string_String, titlestring_String] := 
  Block[{}, varnames = {}; i = 1; ipos1 = findnextnonblank[1, string]; 
    ipos2 = findnextblank[ipos1, string]; 
    varnames = Join[varnames, {tvarname = 
        StringTake[titlestring, {ipos1, ipos2}]}]; 
    While[ipos1 < StringLength[string] && ipos2 < StringLength[string], 
     ipos1 = ipos2; ipos2 = findnextnonblank[ipos2, string]; 
      ipos2 = findnextblank[ipos2, string]; 
      varnames = 
       Join[varnames, {tvarname = StringTake[titlestring, {ipos1, ipos2}]}]]; 
    Return[varnames]]
 
 
 
findnextnonblank[istart_, string_] := 
  Block[{}, i = istart; While[StringTake[string, {i}] == " " && 
      i < StringLength[string], i++]; Return[i]]
 


-------
Joel W. Cannon
Dept. of Physics
Centenary College of Louisiana
P. O. Box 41188
Shreveport, LA 71134-1188

(318)869-5160
(318)869-5026  FAX


  • Prev by Date: Help, looking for a good finance program.
  • Next by Date: DeclarePackage in Mathematica 2.2 for Windows
  • Previous by thread: Help, looking for a good finance program.
  • Next by thread: DeclarePackage in Mathematica 2.2 for Windows