I have files "File1.csv", "File2.csv",.... and "File33.csv". Each of 33 files has a key variable 'date'. I would need to outerjoin these 33 files using 'date' as the key variable. I understand it is a generalization of
B=outerjoin(File1,File2,'Keys', 'date', 'MergeKeys', true)
But I need to understand
(1) how to write a loop to read 33 files from C:\Dropbox (say)
(2) how to write a loop to outerjoin 33 files
(3) how to write a script to save the output file into C:\Dropbox
I will appreciate a code sample.
Thank you.

 採用された回答

Guillaume
Guillaume 2019 年 4 月 10 日

1 投票

Something like (untested, expect typos):
folder = 'C:\dropbox';
filelist = dir(fullfile(folder, 'File*.csv'));
for fileidx = 1:numel(filelist)
filecontent = readtable(fullfile(folder, filelist(fileidx).Name));
if fileidx == 1
joined = filecontent;
else
joined = outerjoin(joined, filecontent, 'Keys', 'Date', 'MergeKeys', true)
end
end
writetable(joined, fullfile(folder, 'joined.csv'));

3 件のコメント

alpedhuez
alpedhuez 2019 年 4 月 10 日
編集済み: alpedhuez 2019 年 4 月 10 日
reference to non-existent field 'Name'.
Error in Untitled (line 4)
filecontent = readtable(fullfile(folder, filelist(fileidx).Name));
Guillaume
Guillaume 2019 年 4 月 10 日
Well, I did warn that typos were likely. You're allowed to try to figure out what the code does and fix the typos yourself.
Replace Name by name.
alpedhuez
alpedhuez 2019 年 4 月 10 日
Yes.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2019 年 4 月 10 日

コメント済み:

2019 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by