Reputation: 131
I have data coming from an oscilloscope, and as I need to make small variations, I end up with a certain number of folders each of which contain two files that I need. The analysis conducted is identical for all folders so at this point on I have to copy paste my entire analysis over and over again.
The structure of my folders is:
C:\Users\................\data\
which contains folders:
10ms
20ms
.
.
.
100ms
Each of the folders has repetitions of the same measurements so for example folder 10ms has:
10ms---> ALL0000 ---> F0000CH1.CSV
F0000CH2.CSV
ALL0001 ---> F0001CH1.CSV
F0001CH2.CSV
ALL0002 ---> F0002CH1.CSV
F0002CH2.CSV
.
.
ALL0004
I was wondering whether it is possible to have mathematica, go to folder 10ms, pick all files ending with CH1.CSV
and those with CH2.CSV
(but distinguish between them), and once done, to go to folder 20ms and repeat all the way to 100ms.
Any ideas?
Upvotes: 2
Views: 1663
Reputation: 131
The method used for the result was the following:
SetDirectory["C:\\ ....................\\ path to where the folders are found]
As explained in the problem, we have 10 folders (10ms, 20ms, 30ms, ...100ms) and each of them have several repeated measurements (ALL0001, ALL0002, .....). Each of those folders has 2 files, one for each of the two channels used.
We begin with a For loop, which will take care of which parent folder the analysis will be conducted on: So the step from 10ms to 20ms to 30ms and so on. Then, the internal loop will take care of importing the desired files and performing the analysis.
For[ i=1, i<11, i++
SetDirectory[ToString[i*10]<>"ms"];
FileNamesch1 = FileNames["*CH1.CSV", {"*"}, Infinity];
FileNamesch2 = FileNames["*CH2.CSV", {"*"}, Infinity];
RawData={};
For[ k=1, k<Length[FileNamesch1]+1, k++
CH1 = Import[FindFile[ToString[FileNamesch1[[k]]]]];
CH2 = Import[FindFile[ToString[FileNamesch2[[k]]]]];
AppendTo[DataRaw,{CH1,CH2}]
ANALYSIS is performed on the files
] (* end of analysis loop *)
SetDirectory[ParentDirectory[]];
] (* End of Directory Loop *)
This way, the external loop is ready to go to the next folder of 20ms and repeat until 100ms.
Upvotes: 1
Reputation: 1
Try using FileNames to get list of files or directories in the current folder. Put its output in the array and go through it with cycle. You need to SetDirectory[] to that directory and than use simple Import. As for files you could use pattern "*CH1.csv" to create filename you want to open. For * you use string expression of the cycle iteration number. The only problem is to add leading zeros. As far I'm concerned where is no built-in function in Mathematica, so I wrote my own. Could share it with you ;)
P.S. A quick hint. Looked at you complete path, that you are placing your data in C:\Users\etc. Try using SetDirectory[NotebookDirectory[]]. This set the current path to that where your .nb file placed. Your could also add subdirectories with FileNameJoin (cross-platform one) or NotebookDirectory<>"\subdirectory" if you Windows user. Also get one level up in path with ParentDirectory[].
Upvotes: 0