check if two tables have the same content if not display the difference in matlab

You can use outerjoin() function to compare two tables and find the differences.

Here's an example code:

main.m
% create two tables with same contents
table1 = table([1;2;3],[4;5;6],'VariableNames',{'Var1','Var2'});
table2 = table([1;2;3],[4;5;6],'VariableNames',{'Var1','Var2'});

% modify one element in one of the tables
table2.Var1(2) = 10;

% outerjoin to compare
difference = outerjoin(table1,table2,'keys','Var1','LeftVariables',{'Var2'},'RightVariables',{'Var2'});

% display the differences
disp(difference);
406 chars
13 lines

The output should be:

main.m
Var1    Var2_left    Var2_right
___________________________
1         4             4      
2         5            10      
3         6             6
150 chars
6 lines

Here, the outerjoin() function compares both tables based on their Var1 variable which contains the same values. The LeftVariables parameter specifies which variables should be included from the left table (table1), and the RightVariables parameter specifies which variables should be included from the right table (table2). The difference table contains the differences between the two tables, in this case, the modified value of Var1 in the table2 is shown.

gistlibby LogSnag