首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多文件读取

多文件读取
EN

Stack Overflow用户
提问于 2016-10-03 22:05:44
回答 1查看 45关注 0票数 0

我在一个文件夹里有10,000多个csv文件,文件名是0,1,2,3.就像这样。我想把它们读一读,然后写进一个文件中作进一步的处理。

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

但没起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-03 22:10:14

与其将它们作为csv文件读取,不如将它们读入原始文件,然后再将它们写入。这可能要快得多。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39841142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档