Extracting values corresponding to exact date from a table and update it in another table on a same date
5 次查看(过去 30 天)
显示 更早的评论
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.
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.
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!