2016-10-03 180 views
0

我在一个文件夹中有超过10,000个csv文件,文件名是0,1,2,3 ......就像那样。我想读它们并写入一个文件进行进一步处理。我试过这个多个文件读取

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

但它没有工作。

+0

你是什么意思“它没有工作”?有错误吗?如果是,请*编辑*您的问题以包含错误。 – hbaderts

+0

allCsv = [] m =长度(文件); (文件名) csvdata = csvread([dname,files(i).name],1,0); allsv = [allCsv; csvdata]; %垂直连接 结束 这一个工作。 – Tab

回答

0

而不是阅读它们作为CSV文件,我只会读取原始文件并再次写出它们。这可能会更快。

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); 
+0

这也适用于其他类型的文件。谢谢 – Tab