MathGroup Archive 2005

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

Search the Archive

Re: Reading from a file.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56698] Re: [mg56678] Reading from a file.
  • From: János <janos.lobb at yale.edu>
  • Date: Wed, 4 May 2005 00:33:11 -0400 (EDT)
  • References: <200505030926.FAA25678@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On May 3, 2005, at 5:26 AM, Namrata Khemka wrote:

> Hi everyone,
>
> I have the following input file:
>
> %Object Name : left asis
>  1      168     0.695873        6.544617        62.457849        
> 82.505554
>  1      169     0.700040        8.332349        61.770844        
> 82.268933
>  1      170     0.704207        9.848946        61.490948        
> 82.065082
>
> %Object Name : right asis
>  2      168     0.695873        -6.179786       37.250156        
> 86.780951
>  2      169     0.700040        -4.581314       36.861102        
> 86.754218
>
> %Object Name : right asis
> 3      168     0.695873        -6.179786       37.250156        
> 86.780951
> 3      169     0.695873        -6.179786       37.250156        
> 86.780951
>
> I want to read this file in the form of a matrix (real numbers), so
> that the matrix has the
> following (eliminate the %Object line while reading):

In[1]:=
SetDirectory[
   "~/Documents/Own solutions"]
Out[1]=
"/Volumes/Data/janos/Document\
s/Own solutions"

In[2]:=
files = FileNames["*.txt"]
Out[2]=
{"Reading from file.txt",
   "StringToNumbers.txt",
   "usgs.txt"}

The first one is your case file:

In[36]:=
fl = ReadList[files[[1]],
     Record];

In fl I have lines of texts.  For example:
In[38]:=
fl[[4]]
Out[38]=
" 1      168     0.695873     \
    6.544617        62.457849  \
      82.505554"

Then, I use that feature that almost every English word has some  
vowels - exceptions like Grrrhhh does not count :)

In[22]:=
mm = (ToExpression[
      StringSplit[#1]] & ) /@
    Pick[fl, (StringFreeQ[#1,
        {"a", "e", "i", "o",
         "u"}] & ) /@ fl]
Out[22]=
{{1, 168, 0.695873, 6.544617,
    62.457849, 82.505554},
   {1, 169, 0.70004, 8.332349,
    61.770844, 82.268933},
   {1, 170, 0.704207,
    9.848946, 61.490948,
    82.065082}, {2, 168,
    0.695873, -6.179786,
    37.250156, 86.780951},
   {2, 169, 0.70004,
    -4.581314, 36.861102,
    86.754218}, {3, 168,
    0.695873, -6.179786,
    37.250156, 86.780951},
   {3, 169, 0.695873,
    -6.179786, 37.250156,
    86.780951}}

For example:

In[27]:=
Head[mm[[1,3]]]
Out[27]=
Real

János


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Re: Reading from a file.
  • Next by Date: Re: Problems with eps format
  • Previous by thread: Re: Reading from a file.
  • Next by thread: Re: Reading from a file.