我在一个文件夹里有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 但没起作用。
发布于 2016-10-03 22:10:14
与其将它们作为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);https://stackoverflow.com/questions/39841142
复制相似问题