Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
its best if your files end with .dat; your command should work as is (?) –  chris Nov 5 '12 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? –  Sjoerd C. de Vries Nov 5 '12 at 20:08
add comment

1 Answer

up vote 4 down vote accepted

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}]
share|improve this answer
    
most probably the right one too ;-) –  chris Nov 5 '12 at 13:26
    
@chris Thanks :) –  Ajasja Nov 5 '12 at 13:36
    
Yes, this is exactly what I wanted. Thanks –  user4524 Nov 5 '12 at 14:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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