Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import data from a file as list/array in Mathematica

I have data(points) that I have to import from another program to Mathematica, so I can plot it. I have control on how the points are going to be written to the file, so I can put them in any way I want. What is the best way to import them to Mathematica? As I'm going to use StreamDensityPlot, the variable I'll have to pass to StreamDensityPlot will have to be in the following way:

data = {
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    ...
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    }

How would you advice me to put the data in the intermediate text file? And what should I use to import it? I've tried Import["mytext.txt", "List"], having my text file with something in the form shown above but it seems as Mathematica considers the points as strings, and I can't do anything with them. Is there a way to convert strings to arbitrary data as is possible in other languages (provided they are valid in that new data type)?

Recap:

  1. Is it possible to convert a string, for example, "5" to a number, in Mathematica? If yes, how?
  2. Is it possible to convert a string like "{1 , 2, 3}" to a list in Mathematica? If yes, how?
  3. Is it possible to load a CSV file as a list of lists as shown above in Mathematica?

Thanks

like image 641
devoured elysium Avatar asked Dec 22 '09 06:12

devoured elysium


People also ask

What is flatten in Mathematica?

Details. Flatten "unravels" lists, effectively just deleting inner braces. Flatten[list,n] effectively flattens the top level in list n times.

How do I import a dataset?

Navigate to the "My Datasets" page by clicking on the corresponding link in the left side of the Public Data Explorer site. Click on the "Choose file" button, and select the location of the zipped, DSPL bundle on your local system. Click the "Upload dataset" button to begin the import process.


2 Answers

Converting Strings to Expressions is done with ToExpression. If you have a plain text file foo.txt, with the formatting as in your example, then just importing it into Mathematica with Get, i.e., << /path/to/foo.txt; will import and evaluate data in the way you want, no need for text to expression translations.

like image 155
Timo Avatar answered Jan 02 '23 21:01

Timo


Try formatting your data file like this:

A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
...

So you can use Mathematica's CSV import. Then partition each row into a list of points after import.

Partition[#, 3]& /@ Import["file.csv", "CSV"]

Also, keep in mind that Mathematica does scientific notation differently than C (or whatever language you're using to write the data file.

like image 31
ragfield Avatar answered Jan 02 '23 20:01

ragfield