Search code examples
matlabequalitycell-array

MATLAB: isequal in conjunction with tables


I am trying to determine equality for two tables. Usually, isequal should work with tables. However, when one of the tables to compare is the content of a cell, then I receive an unexpected result. Take a look at this:

a{1} = table(1,2,3);
b = a{1};

isequal(a,b)

Why is the result false? I would expect the tables to be equal (result true).


Solution

  • Short answer

    A cell is not the same as its contents. Try

    isequal(a{1},b)
    

    Long answer

    To clarify:

    • a is a 1×1 cell array
    • a(1) is its first cell. In this case this is the same as a, because a is 1×1.
    • a{1} is the contents of the first cell, namely a table.

    So isequal(a{1},b) gives true because it compares two tables, and those tables are indeed equal.

    On other hand, isequal(a,b) gives false because a is a cell containing a table and b is a table.

    Note also that

    isequal(a,{b})
    

    would give true, because a is a 1×1 cell array containing table b, and {b} is that table packed into a 1×1 cell cell array, so it is the same.