check if two tables have the same content without keys in matlab

You can compare two tables with the same variables without keys in the following way:

main.m
% create example tables
T1 = table([1;2;3], {'M';'F';'F'}, [true;false;false], 'VariableNames', {'ID', 'Sex', 'Flag'});
T2 = table([3;2;1], {'F';'F';'M'}, [false;false;true], 'VariableNames', {'ID', 'Sex', 'Flag'});
% sort tables by variables
T1_sorted = sortrows(T1, T1.Properties.VariableNames);
T2_sorted = sortrows(T2, T2.Properties.VariableNames);
% check if tables are equal
if isequal(T1_sorted, T2_sorted)
    disp('Tables are equal');
else
    disp('Tables are not equal');
end
487 chars
13 lines

Here we first create two example tables (T1 and T2). Then, we sort both tables by their variable names to put them in the same order. Finally, we use the isequal function to check if the sorted tables are identical. If they are, we print a message that the tables are equal. If they are not, we print a message that the tables are not equal.

related categories

gistlibby LogSnag