Search code examples
matlabfile-read

Multiple file reading


I have more than 10,000 csv file in one folder and file names are 0,1,2,3... like that. I would like to read them and write into one file for further processing.I tried this

files= dir('C:\result\*.csv');
outs = cell(numel(files),1) 
for i = 1:numel(files)   
out{i} = csvread('%i',2,0) 
end 

but it didn't work.


Solution

  • Rather than reading them in as csv files, I would just read in the raw files and write them out again. This will likely be much faster.

    files = dir('C:\result\*.csv');
    filenames = fullfile('C:\result', {files.name});
    
    % Sort the files based on their number
    [~, ind] = sort(str2double(regexp(filenames, '[0-9]+(?=\.csv$)', 'match', 'once')));
    filenames = filenames(ind);
    
    % Open the file that you want to combine them into
    outfile = 'output.csv';
    outfid = fopen(outfile, 'wb');
    
    for k = 1:numel(filenames)
        % Open each file
        fid = fopen(filenames{k}, 'rb');
    
        % Read in contents and remove any trailing newlines
        contents = strtrim(fread(fid, '*char'));
    
        % Write out the content and add a newline
        fprintf(outfid, '%s\n', contents);
    
        % Close the input file
        fclose(fid);
    end
    
    fclose(outfid);