3
votes

I have a lot of files. Every of which contains data. I can happy import one file to Mathematica. But there are more than 500 hundreds of files. I do it so:

 Import["~/math/third_ks/mixed_matrices/1.dat", "Table"];
 aaaa = %
  (*OUTPUT  - some data, I can access them!*)

All that I want is just to make circle(I can do it), but I cannot change name of file - 1.dat. I want to change it.

I tried to make such solution. I generated part of possible names and I have written them to separated file.

Import["~/math/third_ks/mixed_matrices/generate_name_of_files.dat", "Table"];
aaaa = %

Output: {{"~/math/third_ks/mixed_matrices/0.dat"}, \
{"~/math/third_ks/mixed_matrices/1.dat"}, ......

All that I want to do is Table[a=Import[aaaa[[i]] ,{i,1,500}]

But the function Import accepts only String " " objects as filename/paths.

3
By the way there is a Mathematica-specific StackExchange site at mathematica.stackexchange.com. Your questions will be answered faster there, and probably you will get more upvotes! - Verbeia

3 Answers

4
votes

You can use FileNames to collect the names of the data files you want to import, with the usual wildcards.

And then just map the Import statement over the list of filenames.

data will then contain a list comprising the data from each file as a separate element.

data = Import[#,"Table"]& /@ FileNames["~/math/third_ks/mixed_matrices/*.dat"];
2
votes

It's a bit hard to work out what is going on without the file of filenames. However, I think you might be able to solve your problem by using Flatten on the list of filenames to make it a vector of String objects that can be passed to Import. Currently your list is an n*1 matrix, where each row is a List containing a String, not a vector of Strings.

Incidentally you could use Map (/@) instead of Table in this instance.

0
votes

Thank you for your response.
It happened so that I got two solutions in the same time.
I think it would be not fair to forget about second way.

    aaaa = "~/math/third_ks/mixed_matrices/" <> ToString[#] <> ".dat" & /@  Range[0, 116];
   (*This thing generates list of lines  
     Output:
       {"~/math/third_ks/mixed_matrices/0.dat", \
        "~/math/third_ks/mixed_matrices/1.dat", \
        "~/math/third_ks/mixed_matrices/2.dat",    .....etc, until 116

    Table[Import[aaaa[[i]], "Table"], {i, 1, 117}];
    (*and it just imports data from file*)

    bbbb = %;  (*here we have all data, voila!*)

Incidentally, it's not my solution. It was supposed by one my friend: https://stackoverflow.com/users/1243244/light-keeper