MATLAB: Import series of CSV files and perform data crunching

averagingdata importimporting excel data

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?
Thanks.

Best Answer

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);
end
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);
Related Question