Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: [mg4228] Re: help! to input data...
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
John Fultz

Posts: 736
Registered: 12/7/04
Re: [mg4228] Re: help! to input data...
Posted: Jun 26, 1996 2:59 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

> In article <4pit53$jk5@dragonfly.wolfram.com>, tcdoe+@pitt.edu says...
> >
> >well, silly me.
> >I thought i was pretty good at the basics of mathematica, but today i
> >tried to input some numerical data that was comma delimited. i.e.
> >
> >456,-45,21,0,5
> >43,25,3,66,65
> >...
> >...
> >...
> >
> >it is a large data file (10Mb+) with 5 numbers per line. i can't
> >convert the commas to tabs easily.

>
> You don't need to convert the commas to tabs.
>
> Solution #1: Open the file and put a "{" before the first number
> and a "}" after the last. This creats one big list and can be
> read in using something like data = <<"C:\\temp\\data.txt";


BAD!! Don't do this! You will get erroneous data and Mathematica will
never tell you there were any errors. Here's what happens. Create the
sample data file:

{1,2,3,4
5,6,7,8}

Now:

<<"d:\\tmp\\foo"
{1, 2, 3, 20, 6, 7, 8}

There was no comma at the end of the first line, so Mathematica took this as
implicit multiplication. You'll get the wrong data in and never be the wiser
unless you verified the data.

A correct solution for the original data file...that is, something of the
form:

1,2,3,4
5,6,7,8

could be:

Map[First, ReadList["d:\\tmp\\foo", {Number, Character}]]

A more general solution is:

ToExpression[ReadList["d:\\tmp\\foo", Word, WordSeparators->{","}]]

An even more general solution which would allow you to read data in rows
and thus preserve the 2-dimensionality is:

ToExpression[ReadList["d:\\tmp\\foo", Word, WordSeparators->{","},
RecordLists->True]]

However, if memory serves me correctly, the first solution is somewhat
faster if it suits your purpose.

Sincerely,

John Fultz
jfultz@wolfram.com
Applications Group
Wolfram Research, Inc.







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.