MATLAB: I am looking for a generic function that handle any number of csv files

generic function for csv files

I am looking for a help that can provide me a generic function that can work with any number of csv files, each file contains numeric values and texts, I need to obtain just the numeric values, this generic function is crucial for me as I deals with a huge number of csv files, I will be grateful if any can help me in this point. I attached a sample of the type of csv files that I am work with.

Best Answer

Create a list of file names at first, e.g. by:
Folder = 'C:\YouFolder'; % Or however
List = dir(fullfile(Folder, '*.csv'));
File = fullfile(Folder, {List.Name});
Now import the files in a loop:
Result = cell(1, numel(File);
for iFile = 1:numel(File)
fid = fopen(File{iFile}, 'r');
if fid == -1
error('Cannot open file for reading: %s', File{iFile});
end
Data = textscan(fid, '%f%f%f', 'Delimiter', ',', 'Headerlines', 22);
Result{iFile} = cat(2, Data{1:3});
fclose(fid);
end