Message Boards Message Boards

[?] Import a bunch of .csv files automatically?

Posted 7 years ago

Hey guys, I'm currently evualting the experimental results from a digital oscilloscope. This is the code I'm using:

SetDirectory[NotebookDirectory[]];
raw = Import["src/T0005.csv"];
plotData=Select[raw[[17;;]],Length[#]==5&];
time=plotData[[All,1]];
ch1 = plotData[[All,2]];
dataset1=Thread[{time,ch1}];
ListPlot[dataset1,PlotRange->All,AxesLabel->{HoldForm[Zeit in s],HoldForm[Spannung in V]},PlotLabel->HoldForm[Schwingung einer gezupften Saite],LabelStyle->{FontFamily->"Calibri",12,GrayLevel[0]}]

Now the Problem is that I have a lot of csv files and basically all I have to do is to make a plot of all the datasets. So currently I have about 20 files, which all contain the same code, apart from importing another .csv file (T0001.csv, T0002.csv,...). So I thought that there has to be a more elegant way to do the same Operation for a bunch of datasets. Maybe a For-Loop with the couting Parameter in the filename (T000i.csv).

How would you guys solve this Problem?

Attachments:
POSTED BY: Epsilon Tau

assuming your .nb file is in the same directory:

files = FileNames[NotebookDirectory[] <> "*.csv"]

This gives the files to import. If you plan on plotting one vs another, you can get them in one list:

raw = Map[Import, files]

and plot, say raw[[4]] and raw[[9]] on the same plot. or plot them individually by turning all your code above to a function (say plotter[data_] and do:

Map[plotter,raw]

alternatively, if you really do not want all the data in memory at the same time (especially for large files). Change plotter[] to take a filename (plotter[file_]) and do

Map[plotter,files]

In this case:

plotter[file_]:=Module[{raw,plotData,time,ch1,dataset1},
    raw=Import[file];
    plotData=Select[raw[[17;;]],Length[#]==5&];
    time=plotData[[All,1]];
    ch1 = plotData[[All,2]];
    dataset1=Thread[{time,ch1}];
    ListPlot[dataset1,PlotRange->All,AxesLabel->{HoldForm[Zeit in s],HoldForm[Spannung in V]},PlotLabel->HoldForm[Schwingung einer gezupften Saite],LabelStyle->{FontFamily->"Calibri",12,GrayLevel[0]}]]

Regards

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract