I have a lot of text files in my folder (in this example I'm using only 8). I am trying to import all of them and then do calculation on each individual file; I want to use the same notebook without restarting the kernel. So far I managed to import all files in my Mathematica notebook as following:
files = FileNames[NotebookDirectory[] <> "*.txt"];
testList = Import[#, "Data"] & /@ files
the output for testList
is basically all data points {x,y} in all 8 text files.
A very large output was generated. Here is a sample of it:
{{{17.333, 2.667}, {28.667, 2.667}, {42, 2.333}, {51.333, 3.667}, {64,
3.667}, {77.333, 5.333}, {89.333, 5}, {100.667, 5.667}, {94.333,
16.667}, <<2174>>, {760.333, 669.333}, {771.333, 676}, {782,
669}, {792, 675.333}, {802, 668.667}, {761.333, 679.667}, {782.333,
680.667}, {803.333, 680.667}}, <<6>>, {<<1>>}}
The output for files
is:
{"/Users/Desktop/test_automation/400nm_8/8_400nm8_noxy.txt",
"/Users/Desktop/test_automation/400nm_8/1_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/2_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/3_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/4_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/5_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/6_400nm8.txt",
"/Users/Desktop/test_automation/400nm_8/7_400nm8.txt"}
I can access individual files using:
file2 = ReadList["/Users/Desktop/test_automation/400nm_8/1_400nm8.txt"]
The original file consists of two columns and multiple rows in for of {x,y}
but ReadList
outputs all in form of a list not a table with two columns i.e.
{272.5, 1636.69, 2744.44, 4561.31, 3469.94, 5444.25,......}
, so I tried:
File2Table = Table[{file2[[i]][[1]], file2[[i]][[2]]}, {i, 1, Length[file2]}]
I get this error: Part::partd: Part specification 1636.69[[1]] is longer than depth of object.>> General::stop: Further output of Part::partd will be suppressed during this calculation. >>
So first question is how can I get the data files in two columns?
Next, I would like to loop the whole process so that I don't have to copy and paste my code 8 times for each individual file. And since I have a code where I do some calculation for a single text file, I don't know how to define my variables and file names such that Mathematica automatically does calculation for a general case.
Someone suggested to use Module
to define functions in order to do automation.
Thanks a lot for your help