Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Merge two files with different information

1 回表示 (過去 30 日間)
pink flower
pink flower 2020 年 4 月 16 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have two .txt files and I would like to combine the two and put them in one. My files looks like this:
file1.txt
1998 1 1 32.5
1998 1 2 37.2
1998 1 3 40.4
1998 1 5 30.8
file2.txt
1998 1 1 28.2
1998 1 2 35.2
1998 1 4 39.6
1998 1 6 33.0
So, I need an output like this:
1998 1 1 32.5 28.2
1998 1 2 37.2 35.2
1998 1 3 40.4 -
1998 1 4 - 39.6
1998 1 5 30.8 -
1998 1 6 - 33.0
or
1998 1 1 32.5 28.2
1998 1 2 37.2 35.2
1998 1 3 40.4
1998 1 4 39.6
1998 1 5 30.8
1998 1 6 33.0
or
1998 1 1 32.5 28.2
1998 1 2 37.2 35.2
1998 1 3 40.4 NaN
1998 1 4 NaN 39.6
1998 1 5 30.8 NaN
1998 1 6 NaN 33.0
After obtaining this output, remove as lines that do not contain information in the last two columns. Can anybody help me?
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 16 日
I suggest you put the information into a table() object and use innerjoin()

回答 (1 件)

Akira Agata
Akira Agata 2020 年 4 月 21 日
As Walter-san mentioned, the solution would be like this:
% Read .txt files
T1 = readtable('file1.txt','ReadVariableNames',false);
T2 = readtable('file2.txt','ReadVariableNames',false);
% Merge T1 and T2 using innerjoin function
Tall = innerjoin(T1,T2,'Keys',[1 2 3]);

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by