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 tried many different approaches, but I could not solve the following problems, I appreciate your help: What is the best way to read many data files? I mean we have file1.dat, file2.dat, file3.dat …. and I would like to put them in a loop and by using the readlist, import the data, something like what we do in C and sprintf or in bash scripting. I have seen that there is a package which can handle sprintf functionality in Mathematica, but I am looking for simpler solution something like this: (I just used $ sign to show what I mean, like bash)

Do[d$i = ReadList["/path/file$i.dat", Number, RecordLists -> True], {i, 1, 5}]

Can I create a variable like d$i inside the above loop over i? which creates d1, d2, …, and so on, after running the loop?

How can I remove the curly braces from Mean output? I have a single list and the Flatten does not remove the curly braces.

tt = {1, 2, 3, 4, ...} 
Flatten[Mean[tt]] = {1234.1234}
share|improve this question
    
How are you getting list out of Mean? It normally doesn't return a list, but a number. –  m_goldberg Feb 10 at 15:20
    
Just using the Mean command for the single list which is created by ReadList. –  Arash Feb 10 at 15:55

1 Answer 1

up vote 4 down vote accepted

You can do something like this:

First create the Symbols for all the variables you need:

 dd =  Symbol /@ ("d" <> ToString[#]& /@ Range[5])

 ff = "file" <> ToString[#] <> ".dat" & /@ Range[5]

Now read in the file:

Evaluate[dd] = ReadList[#,  Number, RecordLists -> True] & /@ ff;
share|improve this answer
    
Thank you for your reply @RunnyKine. I have tried your method, but I got this error message: SetDirectory["/home/arash/Desktop/test"]; dd = Symbol /@ ("d" <> ToString[#] & /@ Range[9]); ff = "file" <> ToString[#] <> ".dat" & /@ Range[9]; Evaluate[dd] = ReadList["/home/arash/Desktop/test/" <> #, Number, RecordLists -> True] & /@ ff; Set::wrsym: Symbol $Failed is Protected. >> –  Arash Feb 10 at 15:56
    
Since you've SetDirectory to that folder you don't need to include it in ReadList. See my edit and try again without the path to the folder. –  RunnyKine Feb 10 at 16:08
    
Also restart Mathematica and try again. It works for me. –  RunnyKine Feb 10 at 16:12
    
Thank you very much; it works perfectly. –  Arash Feb 10 at 16:20
    
@Arash. Glad I could help. –  RunnyKine Feb 10 at 16:20

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.