MATLAB: Comparing two tables and copy elements of table 2 into table 1

matrix array

Hi,I have two tables as given below.
Table 1:
Table 2
If column1 in table 2 is equal to column 1 in table1 T & W values in table 2 has to copy in table 1, Similar strings has to follow one after the other. If the stings are unequal T & W must be empty. I want the output as below table;
image.PNG

Best Answer

%create demo tables
ID = strsplit('hello new world hi hello hi')';
T = cell(6, 1); W = T;
V = [1; 2; 3; 5; 8; 7];
T1 = table(ID, T, W, V)
T2 = table(ID(4:5), {2;4}, {6;7}, [1;2], 'VariableNames', {'ID', 'T', 'W', 'V'})
%merge the two tables
[found, where] = ismember(T1.ID, T2.ID);
T1(found, {'T', 'W'}) = T2(where(found), {'T', 'W'})