The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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$>, 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, 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


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->{","},

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


John Fultz
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]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.