I have 101 files with data that I want to import into Mathematica. All the file names are numbered as follows datafile.20.0000.out, datafile.20.1000.out, datafile.20.2000.out and so on. I used the solution posted here Importing multiple files using a for-loop

However, the problem I am facing is the zeros at the end. I tried using

z = Table[i, {i, 20, 30, 0.1}]

Data = 
    Import["C:\\Dropbox\\Sims\\datafile." <> 
      ToString[NumberForm[z[[j]], {6, 4}]] <> ".out", "Data"], 
    {j, 20, 30, 0.1}]

But I'm getting error messages.

I would like to retrieve date of each file by Data[[20]], Data[[20.1]] and so on.

  • 1
    $\begingroup$ what is the first file name? is it datafile.20.0000.out? so what is the last file full name? is it datafile.20.1010.out ? You can't have an index that is not discrete. So index 20.1 will not work. $\endgroup$ – Nasser Oct 9 '14 at 4:35
  • 1
    $\begingroup$ Just for starters, Data[[20.1]] is invalid syntax, list indices must be integers. $\endgroup$ – m_goldberg Oct 9 '14 at 4:37
  • $\begingroup$ Yes, the first one is datafile.20.0000 and the last one is datafile.30.0000. Yeah I made a mistake with the index. I suppose Data[[1]], Data[[2]] and so on storing datafile.20.0000, datafile.20.1000 and so on respectively. $\endgroup$ – HuShu Oct 9 '14 at 5:04
  • $\begingroup$ What error messages are you getting? $\endgroup$ – Ymareth Oct 9 '14 at 7:51

A little integer and string hacking generates all and only the names required.

Data = Table[s = ToString[j]; 
  Import["C:\\Dropbox\\Sims\\datafile." <> StringTake[s, 2] <> "." <> 
  StringDrop[s, 2] <> ".out", "Data"], {j, 200000, 300000, 1000}]

You can use FileNames to get list of .out files

data = Import[#, "Data"] &/@ FileNames["datafile.*.out", "C:\\Dropbox\\Sims\\", 1]

You could also use:

data= Import["c:\\dropbox\\sims\\datafile.20." 
<> IntegerString[#, 10, 4] <> ".out" ]& /@ Range[1000]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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