0

I have a folder of 457 .dpt files, which I would like to import in to Mathematica, extract the second column, and combine all 457 into one array, to then save as a .xlsx file. I just can't get the loop right, I've tried For and Do. Also I get the impression that Mathematica isn't great at appending columns so I tried to append them as rows then transpose at the end.

Can anyone help? Thanks.

Clear["Global'*"];

SetDirectory["C:\\Users\\...blahblah...\\TPP 13 Dec 17 (DPT Files)"];

files = FileNames[]; (*gives a list of the 457 filenames*)

out={};

For[i = 0, i < 457, i++,

    data = Import[files[[i]]]

    col = data[[All, 2]] (*Extracts second column*)

    AppendTo[out, {col}]

 ]

outputfile=Transpose[out]

Export["allthedatainonefile.xlsx",outputfile]
Jason B.
  • 68,381
  • 3
  • 139
  • 286
Finlay
  • 1
  • Look at the syntax for Table. You could do out = Table[ Import[ file][[All,2]], {file, files}] – Jason B. Dec 14 '17 at 15:03
  • or Map : Export["path",Import[#][[All,2]]&/@files]; – george2079 Dec 14 '17 at 15:49
  • @george2079 should your code replace everything I have? Or just what's in the for loop? – Finlay Dec 14 '17 at 16:18
  • @JasonB What exact section of my code is your line supposed to replace? I'm not a good programmer, I need this spoon-fed to me I'm afraid. – Finlay Dec 14 '17 at 16:46
  • See the paste here, https://pastebin.com/raw/fvn1fbH4. Any time you find yourself using a For loop, and appending to a list at each step, there is usually a better, more Mathematica way to do it. I included a longer version which is commented out. – Jason B. Dec 14 '17 at 17:01
  • that one line replaces everything after the files= line. (Except I forgot to transpose, so Export["path",Transpose[Import[#][[All,2]]&/@files]]; ) – george2079 Dec 14 '17 at 18:40

0 Answers0