analyse two tables if they have the same length and height if not show the differences and the same content in matlab

You can use the isequal function to compare the two tables. If they have the same size and content, the function would return a logical 1. If you want to get the differences between the two tables, you can use the table datatype and indexing.

Here's an example code snippet to get you started:

main.m
% example data
t1 = table([1;2;3],'RowNames',{'row1';'row2';'row3'},'VariableNames',{'col1'});
t2 = table([1;5;3],'RowNames',{'row1';'row2';'row3'},'VariableNames',{'col1'});

% compare tables
if isequal(t1,t2)
    disp('Tables are the same')
else
    % find differences
    diffRows = t1.col1 ~= t2.col1;
    diffTable = table(t1.col1(diffRows), t2.col1(diffRows), 'VariableNames', {'t1', 't2'}, 'RowNames', t1.Properties.RowNames(diffRows));
    disp('Tables are different')
    disp(diffTable)
end
501 chars
15 lines

In this example, we create two tables t1 and t2 of the same size, but with different content. We use isequal to check whether the two tables have the same content. Since they are different in this case, we find the differences in the col1 variable between the two tables using logical indexing and create a new table diffTable with the differing rows and their values in each table.

related categories

gistlibby LogSnag