# Importing 2d array of files

I would like to import multiple files to a 2d array. I know to get a list of files I can do

myfiles=FileNames["*.txt"]
alldata=Import/@myfiles


Is it possible to get my data files in a 2d array if, for example, my files look like

myfiles={file11.txt,file12.txt,file21.txt,file22.txt}


where the numbers in the filenames correspond to an x and y index. I have done this using for loops. Is there a more functional way?

• its best if your files end with .dat; your command should work as is (?) Nov 5, 2012 at 12:32
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2)Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign. Another suggestion: a nice user name? Nov 5, 2012 at 20:08

A different interpretation:

Yes, it's possible and easy using GatherBy

myfiles = {"file11.txt", "file12.txt", "file21.txt", "file22.txt"};
t = GatherBy[myfiles, {StringTake[#, -5] &}] // Transpose

(* ==> {{"file11.txt", "file12.txt"}, {"file21.txt", "file22.txt"}} *)

t[[1, 1]]

(* ==> "file11.txt" *)

t[[2, 1]]

(* ==> "file21.txt" *)

(* import with a double map*)
Function[fs, Import /@ fs] /@ t

(*or the much more elegant way of importing -- one just has to read the docs:) *)
Map[Import, t, {2}]
`
• most probably the right one too ;-) Nov 5, 2012 at 13:26
• @chris Thanks :) Nov 5, 2012 at 13:36
• Yes, this is exactly what I wanted. Thanks Nov 5, 2012 at 14:12