New Reply

Importing data into mathematica

 
Share Thread Thread Tools
Apr17-11, 02:45 PM   #1
 

Importing data into mathematica


I'm having trouble getting data into mathematica.

I have a text file will a list with members seperated by returns (enters) that I want to import as an array. I tried:

Import["data.txt", "Table"]
Import["data.dat", "Table"]
Import["data.txt"]
Import["data.dat"]

All of which failed to find the file, even though it is stored in the same directory. Finally I tried

Import["C:/Users/.../data.txt", "Table"]
(the ... is all the folders in between)

And it gives me this message (not an error, its just this is all it outputs):

System`Convert`TableDump`GetData(System`Convert`TableDump`ImportTable(C :\Users\Darin\Documents\Darin Live Sync Folder\Organizations\MIT\Classes\5_74 QMII\PSet 5\data.txt))

I'm pulling my hair out trying to get this thing to work, any guidance would be deeply appreciated.

The .nb file is stored in the same directory as the data file.

Thanks
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> King Richard III found in 'untidy lozenge-shaped grave'
>> Google Drive sports new view and scan enhancements
>> Researcher admits mistakes in stem cell study
Apr17-11, 04:41 PM   #2
 
I tried with a .csv file as well, same message, no table.
Apr17-11, 06:37 PM   #3
 
Try:
SetDirectory[NotebookDirectory[]]
Import["TestFile.txt", "Table"]
Apr17-11, 06:40 PM   #4
 
Mentor

Importing data into mathematica


I would use the SetDirectory command first. Also, be aware that you have to change the slashes to \\ so that it will parse correctly.
Apr18-11, 02:06 PM   #5
 
Thanks, I tried both those things, same message:

System`Convert`TableDump`GetData(System`Convert`TableDump`ImportTable(C :\Users\...\Documents\...\data.csv))

Incidentally, it treats both \\ and / the same and parses both 'correctly' in that it understands the input and seems to be going to right place. If I use \ then I get an error message, and if I don't put in a file name that is there I get a not found message.

So it is finding the file, and is not giving an error, but its not turning it into an array like I want. The file is just the numbers separated by commas (or in the text file case by returns).
Apr18-11, 02:54 PM   #6
 
This may be somewhat Mathematica-version dependent, but let's see what we can do for you.

I created a tiny text file containing 3 characters
1\n2
where \n represents tapping the Enter key.

To simplify this whole directory business I put that in my C:\Program Files\Wolfram Research\Mathematica\x.y directory, where x.y is my current version number. You can test using that method or if you have your directory handling figured out you can ignore this.

I then hopped into Mathematica and did this (literally copying and pasting exactly my notebook cell contents). Note: My textual description not preceded by In[] or Out[] was not part of my notebook contents, it is just to explain what is going on.

In[1]:= Import["numbers.txt"]
Out[1]=
1
2

But Mathematica will often "lie to you", presenting things that look like one thing but are another. FullForm will more often show you what something really is.

In[2]:= FullForm[%]
Out[2]//FullForm= "1\n2"

So that read the file as a single string and that isn't convenient.

So then I did exactly this

In[3]:= Import["numbers.txt","Lines"]
Out[3]= {1,2}

That looks exactly like what I think you want, but I bet it isn't.

In[4]:= FullForm[%]
Out[4]//FullForm= List["1","2"]

That is a list of strings and you almost certainly want a list of numbers. So ToExpression will help you.

In[5]:= ToExpression[%]
Out[5]= {1,2}

In[6]:= FullForm[%]
Out[6]//FullForm= List[1,2]

You can then bundle this up and also verify the format using this.

In[7]:= mynumbers=ToExpression[Import["numbers.txt","Lines"]]
Out[7]= {1,2}

In[8]:= FullForm[mynumbers]
Out[8]//FullForm= List[1,2]

As a general rule, I think making a copy of your notebook, editing the copy, and any needed data files, down to the point where it contains everything needed and nothing more for someone else who knows nothing about what is already in your head to be able to demonstrate exactly what the problem or the solution is, restarting Mathematica, or killing the kernel, evaluating all the cells in the notebook in order and then carefully copy-and-pasting all the notebook into a posting is the most likely way to get a good solution on the first try.

Are there at least a dozen different ways of accomplishing almost anything in Mathematica, including at least three that are completely incomprehensible? Yes.
Apr19-11, 08:46 AM   #7
 
Thank you so much for the advice and help,

I did manage to get it to work, by using SetDirectory and removing the "Table" specification from the Input command, it bring in the data in what I believe is a 65536x1 list (when I call Dimensions[omega], where omega is what I name the input, it gives me (65536,1), and I can get elements of the list via omega[[element index]]) so it looks like everything is hunky-dorie!

I don't know why it doesn't work without the SetDirectory command, or why I can't specify table (or array) when I use the setDirectory command, but in either case this works now.

Thank you for your help, BillSimpson I'm sorry I didn't end up using your algorithm, I appreciate the thought that went into it though, thank you for your consideration,

rynlee
New Reply
Thread Tools


Similar Threads for: Importing data into mathematica
Thread Forum Replies
Importing data in maple Math & Science Software 1
Easy: importing a batch of data into MATLAB Math & Science Software 5
Matlab - importing data from excel & interpolating Programming & Comp Sci 1
Importing data into MATLAB Math & Science Software 0
Techniques for successfully importing Excel data into Access Computing & Technology 0