4
$\begingroup$

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}
$\endgroup$
2
  • $\begingroup$ How are you getting list out of Mean? It normally doesn't return a list, but a number. $\endgroup$
    – m_goldberg
    Commented Feb 10, 2014 at 15:20
  • $\begingroup$ Just using the Mean command for the single list which is created by ReadList. $\endgroup$
    – O_o
    Commented Feb 10, 2014 at 15:55

1 Answer 1

4
$\begingroup$

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;
$\endgroup$
5
  • $\begingroup$ 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. >> $\endgroup$
    – O_o
    Commented Feb 10, 2014 at 15:56
  • $\begingroup$ 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. $\endgroup$
    – RunnyKine
    Commented Feb 10, 2014 at 16:08
  • $\begingroup$ Also restart Mathematica and try again. It works for me. $\endgroup$
    – RunnyKine
    Commented Feb 10, 2014 at 16:12
  • $\begingroup$ Thank you very much; it works perfectly. $\endgroup$
    – O_o
    Commented Feb 10, 2014 at 16:20
  • $\begingroup$ @Arash. Glad I could help. $\endgroup$
    – RunnyKine
    Commented Feb 10, 2014 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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