How to append labels from one table to another based on certain conditions?

1 次查看(过去 30 天)
I have 2 tables in MATLAB, table X and Table Y each of different dimensions. Table X and Table Y both contain a column named Code. Table X has the last column as LabelX (an empty column) and Table Y has the Last Column LabelY (with label values as strings). What I would like to achieve is that for all elements in Table X, I would like to add the Label from Table Y's LabelY corresponding to the Code. So, for example if Code is 1 in table X, MATLAB would look for the LabelY in Table Y's LabelY column and then append the same label to Table X's LabelX Column. I tried using for loops for this purpose but I don't feel that it is a good way or if I am doing it correctly. Any help in this regard would be appreciated.
for i = 1:size(TableX,1)
for j = 1:size(TableY,1)
if(TableX.Code(i) == TableY(j))
TableA.Label(i) = LDTUniqueAlarmListv3.FunctionalGrouping(j);
end
end
end

采纳的回答

Guillaume
Guillaume 2018-12-4
Sounds like you want a left outerjoin between your two tables:
joinedTable = outerjoin(tableA, tableB, 'Keys', 'Code', 'Type', 'left', 'MergeKeys', true)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by