Combining excel files with unequal rows

조회 수: 2 (최근 30일)
Katelyn
Katelyn 2024년 4월 9일
댓글: Dyuman Joshi 2024년 4월 9일
I have two excel files which I would like to combine into a single file. The data is as follows
:
I would like to combine these two tables into a single table with the columns "Name" "Date" "Value 1" "Value2" where the occassion of John 04-05-2011 shows a value for Value 1 but remains empty for Value 2.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 4월 9일
편집: Dyuman Joshi 2024년 4월 9일
Try outerjoin -
%Data from the images attached for example
%Use readtable() to read the data as tables directly
Name = ["Adam" "Adam" "John" "John" "John" "Karen"].';
Date = datetime(2010, [11 12 11 12 5 5], [5 6 5 6 4 4]).';
Value1 = [12 16 8 3 14 12].';
t1 = table(Name, Date, Value1)
t1 = 6x3 table
Name Date Value1 _______ ___________ ______ "Adam" 05-Nov-2010 12 "Adam" 06-Dec-2010 16 "John" 05-Nov-2010 8 "John" 06-Dec-2010 3 "John" 04-May-2010 14 "Karen" 04-May-2010 12
Name = ["Adam" "Adam" "John" "John" "Karen"].';
Date = datetime(2010, [11 12 11 12 5], [5 6 5 6 4]).';
Value2 = [8 6 1 3 8].';
t2 = table(Name, Date, Value2)
t2 = 5x3 table
Name Date Value2 _______ ___________ ______ "Adam" 05-Nov-2010 8 "Adam" 06-Dec-2010 6 "John" 05-Nov-2010 1 "John" 06-Dec-2010 3 "Karen" 04-May-2010 8
%join tables
out = outerjoin(t1, t2, 'Keys', [1 2])
out = 6x6 table
Name_t1 Date_t1 Value1 Name_t2 Date_t2 Value2 _______ ___________ ______ _________ ___________ ______ "Adam" 05-Nov-2010 12 "Adam" 05-Nov-2010 8 "Adam" 06-Dec-2010 16 "Adam" 06-Dec-2010 6 "John" 04-May-2010 14 <missing> NaT NaN "John" 05-Nov-2010 8 "John" 05-Nov-2010 1 "John" 06-Dec-2010 3 "John" 06-Dec-2010 3 "Karen" 04-May-2010 12 "Karen" 04-May-2010 8
  댓글 수: 4
Katelyn
Katelyn 2024년 4월 9일
Thank you so much! I tried this and it worked perfectly.
Dyuman Joshi
Dyuman Joshi 2024년 4월 9일
Glad to have helped!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by