MATLAB: Read Praticular set of file from multiple .csv files and extract a specific set of columns from it.

csvimporting excel dataMATLAB

Problem Statement:
I have 15 .csv files in the target folder.
1) Out of 15 I would want to read 5 selected files at a time.
2) From Those 5 files I only want extract data from columns 9 an 10 from each file and store it in a new file or array for further processing.
3) Repeat.
Can some please help me figure out the solution code to this problem.
Thank You!

Best Answer

projectdir = 'targerfoldername';
dinfo = dir(fullfile(projectdir, '*.csv'));
filenames = fullfile(projectdir, {dinfo.name});
numfiles = length(filenames);
filedata = cell(numfiles, 1);
for K = 1 : numfiles
thistable = readtable(filenames{K});
filedata{K} = thistable{:,[9 10]};
end
AtATime = 5;
maxiter = 20;
results = cell(maxiter, 1);
for iteration = 1 : maxiter
rand5 = randperm(numfiles, AtATime);
combined_5 = vertcat( filedata{rand5} );
now do something with the combined data
results{iteration} = whatever
end