How to combine hundreds of CSV files in Matlab?

61 ビュー (過去 30 日間)
Maksim Sorin
Maksim Sorin 2022 年 3 月 21 日
回答済み: Voss 2022 年 3 月 21 日
Hi all,
I have LOTS of data in CSV files and I'd like to create one massive master CSV file. How can I combine all of the CSV files into one? Each CSV file has a header with time, type of data, etc. They all have the same number of columns but differet numbers of rows.

採用された回答

Voss
Voss 2022 年 3 月 21 日
Here's an example of how to do it (with 2 files):
input_path = '.'; % location of .csv files
output_file = 'result.csv'; % name of file containing combined data
% read each .csv file into a table stored in a cell array of tables
% called 'all_data':
file_info = dir(fullfile(input_path,'*.csv'));
full_file_names = fullfile(input_path,{file_info.name});
n_files = numel(file_info);
all_data = cell(1,n_files);
for ii = 1:n_files
all_data{ii} = readtable(full_file_names{ii});
end
% check the tables:
all_data{:}
ans = 4×2 table
time type ____ _____ 0 {'A'} 1 {'B'} 2 {'C'} 3 {'B'}
ans = 3×2 table
time type ____ _____ 0 {'B'} 1 {'A'} 2 {'C'}
% concatenate all the tables into one big table, and write it to
% output_file:
writetable(cat(1,all_data{:}),output_file);
% check that the resulting output file exists:
dir('*.csv')
data_1.csv data_2.csv result.csv
% check the contents of the resulting output file:
readtable(output_file)
ans = 7×2 table
time type ____ _____ 0 {'A'} 1 {'B'} 2 {'C'} 3 {'B'} 0 {'B'} 1 {'A'} 2 {'C'}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by