accessing a .csv file with a certain name in a folder and calculating its average, but doing this for several folders in subfolders.

1 view (last 30 days)
I have a main folder
\MainFolder\
within this folder are individual folders for each participant (32)
100514, 120414, .....310513 [all six digit ids]
participant = 1:size(nameFolds,1)
within each participant folder, there is another folder called Detection results
In this folder there are 40 folders '1_1', '1_2', ....'1_40'.
Within each of these are .csv files. I would like to take the file with name ending in
1_1_FixationDuration.csv
1_1_saccadeAmplitude.csv
1_1_saccadeDuration.csv
each of these contain a list of numbers and I would like to output the number of items and their average in a different csv file.
The code that has worked for me for one folder is
folder = 'D:\.....\MainFolder\100514\DetectionResults\1_1\';
csvFiles = dir(fullfile(folder, '*.csv')); % Using absolute path names
numfiles = length(csvFiles);
average = zeros(1, numfiles);
for k = 1:numfiles
M = csvread(fullfile(folder, csvFiles(k).name));
average(k) = mean(M(:,1));
end
csvwrite(fullfile(folder_out, 'output.csv'), average);
but i would like this to loop through the several folders and read those files ending in [1_1_FixationDuration.csv, 1_1_saccadeAmplitude.csv
1_1_saccadeDuration.csv] in particular, not any other .csv that may exist.

Answers (1)

DrZoidberg
DrZoidberg on 28 Jan 2020

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!