Extracting values corresponding to exact date from a table and update it in another table on a same date
조회 수: 2 (최근 30일)
이전 댓글 표시
Muhammad Haris Siddiqui
2021년 11월 4일
댓글: Muhammad Haris Siddiqui
2021년 11월 4일
Hello,
I have two tables with different number of rows.
I want to extract the values from table "ntt" from "VarName4" column as shown below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/789872/image.png)
and paste it in column 2 of table "C" as shown below corresponding to the exact date as in table "ntt". As table "C" have other dates for which I don't have data and want to leave it blank or with zeros.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/789876/image.png)
I treid to use for loop for updating a table "C"
for k = 1:height(ntt)
p = ntt{k,"Rainfallmm"};
C(p,2) = ntt{k,"VarName4"};
end
but it gives me this error
"A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, or a cell array of character vectors"
any help would be appreciated.
Thanking you in anticipation
댓글 수: 0
채택된 답변
Seth Furman
2021년 11월 4일
1) Convert your tables to timetables
This isn't strictly necessary to answer your question, but is generally a good idea when working with tabular timestamped data.
% Example tables
Date = datetime(2021, 1, 1:10:100)';
t1 = table(Date, (1:10)')
Date = datetime(2021, 1, 1:5:100)';
t2 = table(Date)
% Convert tables to timetables
t1 = table2timetable(t1)
t2 = table2timetable(t2)
2) Take a look at the functions (join, innerjoin, and outerjoin)
I suspect that you'll want outerjoin in your case.
outerjoin(t1, t2, "Keys", "Date")
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!