Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!