2016-10-03 179 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