Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Reading from a file.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56697] Re: [mg56678] Reading from a file.
  • From: "Tomas Garza" <tgarza10 at msn.com>
  • Date: Wed, 4 May 2005 00:33:09 -0400 (EDT)
  • References: <200505030926.FAA25678@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

No doubt there are better ways, but this works: Have your file as a Tab 
separated file (in Excel, perhaps) witha a txt extension

In[1]:=
a = ReadList["tst.txt", Word, NullWords -> True, RecordLists -> True,
RecordSeparators -> {"\n"}, WordSeparators -> "\t"]


Out[1]=
{{%Object Name: left asis, , , , , }, {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}, {}, {%Object Name: right asis, , , , , }, {2, 168,
0.695873, -6.179786, 37.250156, 86.780951}, {2, 169, 0.70004, -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}}

In[2]:=

ToExpression[Select[DeleteCases[a,
    x_ /; x == {}],
   StringTake[#1[[1]], 1] != "%" & ]]
Out[2]=
{{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}}



Tomas Garza

Mexico City
----- Original Message ----- 

From: "Namrata Khemka" <namrata.khemka at gmail.com>
To: mathgroup at smc.vnet.net
Subject: [mg56697] [mg56678] Reading from a file.


> 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):
>
> 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
> 2      168     0.695873        -6.179786       37.250156       86.780951
> 2      169     0.700040        -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
>
>
> So the real numbers matrix is of size [7][6]
>
> I have tried quite a few things but nothing seems to be working.
>
> Thanks in advance for the help,
>
> Namrata
>
> 


  • Prev by Date: how call a function by same name in 2 different contexts?
  • Next by Date: Re: Reading from a file.
  • Previous by thread: Re: Reading from a file.
  • Next by thread: Re: Reading from a file.