check if two tables are the same if not show the differences without keys in matlab

One way to check if two tables are the same is to use the isequal function in Matlab. This function returns a logical value true if both inputs are the same size and contain the same data elements; otherwise, it returns false.

If the tables are not the same and you want to show the differences without using the keys, you can use the table2array function to convert the tables to arrays and then compare them using the == operator to get a logical array that indicates where the values in the tables are different. The array2table function can then be used to convert the logical array back to a table with the same size and variable names as the original tables.

Here's an example code snippet that demonstrates how to do this for two tables t1 and t2:

main.m
% Check if tables are the same
if isequal(t1, t2)
   disp('Tables are the same');
else
   % Find locations where values in tables are different
   diffArray = table2array(t1) ~= table2array(t2);
   % Convert logical array back to table
   diffTable = array2table(diffArray, 'VariableNames', t1.Properties.VariableNames);
   % Display table with differences
   disp(diffTable);
end
381 chars
12 lines

Note that this method only works if the two tables have the same number of rows and variables with the same variable names.

related categories

gistlibby LogSnag