Message Boards Message Boards

1
|
16528 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Import multiple files using loop

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 http://mathematica.stackexchange.com/questions/22357/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 = 
  Table[
    Import["C:\\Dropbox\\Sims\\datafile." <> 
      ToString[NumberForm[z[[j]], {6, 4}]] <> ".out", "Data"], 
    {j, 20,30}]

But I'm getting error messages.

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

POSTED BY: Nilanjan Banik
5 Replies

not a problem, use another string selection pattern, like the following

data =  Map[Import[#, "Data"]&, FileNames["datafile.20.*.out", {"C:\\Dropbox\\Sims\\"}]]

Thank your for your reply, but the problem is simulation's output directory also contains 200 other massfunction.out, mergerhistories.out files which I don't want.

POSTED BY: Nilanjan Banik

Thanks for your reply. I wish that was feasible, in fact the best way would be to first use MS Excel and do most of the pre-processing (remove/add/calculate columns and rows etc) of the data and import as an .xls sheet, but I have 101 files, my next simulation run will have even more, and also I would want more automation to the entire system, ie from running the simulation to plotting the results in a BubbeChart. That's why I am looking for a loop statement.

POSTED BY: Nilanjan Banik

The first thing I would do is to use FileNames[] to generate names of files, so you don't need string manipulation.

 data =  Map[Import[#, "Data"]&, FileNames["*.out", {"C:\\Dropbox\\Sims\\"}]]

or, using a table

files = FileNames["*.out", {"C:\\Dropbox\\Sims\\"}];
data = Table[Import[files[[j]]], {j, 1, Length[files]}]

Hope this helps, Roberto

Posted 11 years ago

I think that import of many files to your Mathematica Kernel (if it is not necessary) is not good idea. Better way is import one file and do something (preprocessing) and store result. After then clear data and import next file. For my advice I recommand you use $HistoryLength=0;

If you would like only correct your code then solution is there:

Data = 
  Table[
    Import["C:\\Dropbox\\Sims\\datafile." <> 
      ToString[NumberForm[j, {6, 4}]] <> ".out", "Data"], 
    {j, 20,30,0.1}]
POSTED BY: Mirek M
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract