how to replace columns in a table with another table in matlab

Assuming you want to replace the entire columns of Table1 with the corresponding columns of Table2, you can do the following:

main.m
% create example tables
Table1 = table([1; 2; 3], [4; 5; 6], [7; 8; 9], 'VariableNames', {'A', 'B', 'C'});
Table2 = table([10; 11; 12], [13; 14; 15], [16; 17; 18], 'VariableNames', {'X', 'Y', 'Z'});

% replace columns of Table1 with columns of Table2
Table1(:, :) = Table2(:, :);
280 chars
7 lines

Note that this will only work if Table1 and Table2 have the same number of columns. If you want to replace only some of the columns, you can use indexing to select the columns you want to replace:

main.m
% create example tables
Table1 = table([1; 2; 3], [4; 5; 6], [7; 8; 9], 'VariableNames', {'A', 'B', 'C'});
Table2 = table([10; 11; 12], [13; 14; 15], [16; 17; 18], 'VariableNames', {'X', 'Y', 'Z'});

% replace columns 'A' and 'C' of Table1 with columns 'X' and 'Z' of Table2
Table1(:, {'A', 'C'}) = Table2(:, {'X', 'Z'});
322 chars
7 lines

related categories

gistlibby LogSnag