How can I add a column to a table based on each csv files name?
2 ビュー (過去 30 日間)
古いコメントを表示
I want to write a bunch of csv files together and I am using the following code:
xx = table();
CSVfileNames = dir('*.csv');
for kk = 1:numel(CSVfileNames)
x0 = readtable(CSVfileNames(kk).name);
xx = [xx; x0];
oo=table2array(xx)
xlswrite('AllCSV',oo)
end
However, within the file I would like an additional column which has the csv file name.
So for example, the first 10 rows would be named the first csv file, the next 10 rows would be the next csv file name etc.
I tried
xx = addvars(xx,CSVfileNames,'Before','Var1');
- but this didnt work.
I am very new to Matlab so please bear with me! I found this code online and have adapted.
0 件のコメント
採用された回答
Walter Roberson
2020 年 5 月 4 日
filename = CSVfileNames(kk).name;
x0 = readtable(filename);
x0 = addvars(x0, repmat({filename}, height(x0), 1), 'Before', 'Var1', 'NewVariableNames', 'filename');
3 件のコメント
Walter Roberson
2020 年 5 月 6 日
'ReadVariableNames',false should solve the problem. However, before R2020a, you might want to do a detectImportOptions on the first file and pass those options to all of the readtable().
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!