MATLAB Answers

Import series of CSV files and perform data crunching

1 view (last 30 days)
pk94engineer on 17 Sep 2020
Edited: Mohammad Sami on 18 Sep 2020
I have csv data files sequentially and individually labelled as v0,v10,v20,v30,v40......v10000 in a folder. So in all its 1002 .csv files. I want to import all the data from these files each of which has 2730 rows (numerical data) X 6 colmns. Additionally with the import, I'd like to perform an even average over say every 50 sequential files to store it in a variable X which will have a size of 54600 R x 120 C (i.e 20 x 2730 R and 20 x 6) (Guess I'm correct here).
Request your assistance to perform this.
I faced some issues running a for loop with csvread and xlsread. It might be bcecause my first 6 lines in every file has header and whitespaces in it just before the data commences?

Accepted Answer

Mohammad Sami
Mohammad Sami on 18 Sep 2020
Edited: Mohammad Sami on 18 Sep 2020
You need to set the NumHeader Lines to ignore the first 6 lines.
folderwcsv = 'somepath';
csvs = dir(fullfile(folderwcsv,'*.csv')); % assumes the csv are not in subfolders
data = cell(length(csvs),1);
for i = 1:length(csvs)
data{i,1} = readtable(fullfile(csvs(i).folder,csvs(i).name),'NumHeaderLines',6);
If the readtable doesnot work for you be default, it allows you to set all the options.
Two ways to set the options are to use either detectImportOptions or manually specificy them like follows
% if your text files are delimited
opts = delimitedTextImportOptions('NumVariables',6,'DataLines',7,'Delimiter',',');
%% for %%
data{i,1} = readtable(fullfile(csvs(i).folder,csvs(i).name),opts);


Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!