Mathematica Importing data into mathematica

  • Thread starter Thread starter rynlee
  • Start date Start date
  • Tags Tags
    Data Mathematica
Click For Summary
The discussion centers around difficulties importing data into Mathematica from text files. The user initially attempts various import commands but encounters issues with file recognition, even when the files are in the same directory as the notebook. Suggestions include using the SetDirectory command to set the working directory to the notebook's location and ensuring the correct file path format. The conversation reveals that importing data as "Lines" instead of "Table" can yield better results, as it allows for a straightforward conversion of the data into a usable array. The user ultimately resolves the issue by using SetDirectory and omitting the "Table" specification, successfully importing the data as a list. The discussion highlights the importance of directory management and the nuances of Mathematica's import functions.
rynlee
Messages
44
Reaction score
0
I'm having trouble getting data into mathematica.

I have a text file will a list with members separated 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
 
Physics news on Phys.org
I tried with a .csv file as well, same message, no table.
 
Try:
SetDirectory[NotebookDirectory[]]
Import["TestFile.txt", "Table"]
 
I would use the SetDirectory command first. Also, be aware that you have to change the slashes to \\ so that it will parse correctly.
 
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).
 
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.
 
Last edited:
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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K