0
$\begingroup$
 list1 = OpenRead[
   "/home/sachin/Downloads/mam_code_evans/neutral_fix/n_t/nt.txt"];
 dataa = ReadList[list1, {Number, Number, Number}];
y1 = dataa[[All, 2]];
y2 = Table[1000, {i, 1, 100000}];
 y3 = Flatten[{y1, y2}];

I have 100 nt1.txt, nt2.txt, nt3.txt ... How to read these 100 files and save them in a 100 lists (in a loop) and then take average?

$\endgroup$
1
  • $\begingroup$ Maybe you can try the manipulation on list of strings. Use Table or some functions to build a list with elements are path and name of your data. Then finally map OpenRead over it. $\endgroup$
    – swish47
    Oct 26, 2022 at 20:28

1 Answer 1

1
$\begingroup$

Read the data with memoization instead of reading/saving manually in variables:

Clear[data];
data[n_Integer?Positive] := data[n] = 
  ReadList["~/Downloads/mam_code_evans/neutral_fix/n_t/nt" <> 
           ToString[n] <> ".txt", {Number, Number, Number}]

Now we can load them and take the average (no need to save them to list; this is done internally in Mathematica):

Array[data, 100] // Mean
$\endgroup$
2
  • $\begingroup$ This is not of much use to me. I want using the way i asked in the question. Because then i have to use the data somewhere else. $\endgroup$ May 31, 2022 at 6:42
  • 2
    $\begingroup$ You can use data[51] fror example in any other place you want. Or store A = Array[data, 100]. Or... $\endgroup$
    – Roman
    May 31, 2022 at 8:13

Your Answer

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

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