10
$\begingroup$

once again I am stuck solving a seemingly easy problem :(

I would like to make a List of tables by importing data from several txt-Files. Importing the data into a table works so far and I am able to do ListPlots etc.

The txt-file-names follow a simple pattern: txtFile_1.txt, txtFile_2.txt, ...

My take on the problem is the following:

    testList = List[1, 2, 3, 4]
    For[i = 1, i < 4, i++
        testList[[i]] = 
       Import["C:\\txtFiles\\txtFile_" <> ToString[i] <> ".txt" <> 
         ToString[i] <> ".txt", "Data"];
     ]

Background: I would like to have the tables in the list to be able to access them via testList[[i]] and in order to do consecutive ListPlots via a for-loop etc...

$\endgroup$
1
  • $\begingroup$ Importing with Table will build the desired testList. $\endgroup$
    – BoLe
    Mar 30, 2013 at 13:22

3 Answers 3

12
$\begingroup$

The short answer is "don't use a For loop".

The longer answer is: you can do this using Table and you definitely don't need to pre-populate testlist. Instead do something like this:

testlist = 
  Table[Import["C://txtfile_" <> ToString[i] <> ".txt", "Data"], {i, 4}]

You can then access the imported files as testlist[[1]] and so on.

Asides from being shorter code, this approach is more efficient. You could use the file system interrogation functions to work out how many files there are with the right file name pattern, and use that to define the Table iterator.

$\endgroup$
1
  • $\begingroup$ thank you - this is pretty much straight forward and exactly what I have been looking for. $\endgroup$ Mar 30, 2013 at 13:41
13
$\begingroup$
testList = Table[Import["C:\\txtFiles\\txtFile_" <>
    ToString[i] <> ".txt", "Data"], {i, 4}]

Or, you can map over the files you want to import. Say you have them in the directory where your notebook is saved.

files = FileNames[NotebookDirectory[] <> "*.txt"];
testList = Import[#, "Data"] & /@ files

Both times you will get the list that you want and access.

$\endgroup$
5
  • $\begingroup$ wow - this is easier than I would have thought. I wonder how you gus find out how to do things like that. The mathematica-help isn't that explicit here !! $\endgroup$ Mar 30, 2013 at 13:40
  • $\begingroup$ Also, note that Table iterator doesn't need to be an increasing integer, but it can go over a list of files. Table[Import[file, "Data"], {file, files}]. $\endgroup$
    – BoLe
    Mar 30, 2013 at 13:41
  • $\begingroup$ @Probabilitnator Don't agree, I think help is very helpful in Mathematica. But one has to spend some time on it. ;) $\endgroup$
    – BoLe
    Mar 30, 2013 at 13:44
  • 2
    $\begingroup$ @Probabilitnator, here's a good place to start: mathematica.stackexchange.com/questions/18/… $\endgroup$
    – RunnyKine
    Mar 30, 2013 at 16:48
  • $\begingroup$ @RunnyKine thank you :) $\endgroup$ Apr 1, 2013 at 9:27
10
$\begingroup$

The files don't need to be named nicely, either. Say you have a list of filenames:

allNames = {"filename.txt", "thatFile.txt", "thisfile.txt", ... };

Now you can read them all in:

allData = Table[Import[allNames[[i]],"Data"],{i, 1, Length[allNames]}];

It is not even necessary to use an integer iterator, as in:

allData = Table[Import[name, "Data"], {name, allNames}];
$\endgroup$
1
  • 1
    $\begingroup$ Actually it's not even necessary to use Table: Map AKA /@ will suffice. $\endgroup$
    – Ruslan
    Jan 1, 2018 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.