Indexing a cell array in a table

28 views (last 30 days)
When I have a table where one of the variables is a cell array, indexing using curly brackets into that column does not seem to work as expected.
myCell = {'a';'b';'c';'d';'e'};
myTable = table(myCell)
isequal(myCell{1},vertcat(myTable.myCell{:})) % Should be false, but returns true
isequal(vertcat(myCell{:}),vertcat(myTable.myCell{:})) % Should be true, but returns false
It seems to me that using curly brackets to index into a cell array returns only the first cell indexed.
isequal(myTable.myCell{2:3},myCell{2}) % returns true
This seems like a bug, or am I missing something?

Accepted Answer

Debarati Banerjee
Debarati Banerjee on 13 Apr 2015
When you try to access contents of multiple cells, MATLAB creates a comma-separated list. Because each cell can contain a different type of data, you cannot assign this list to a single variable. However, you can assign the list to the same number of variables as cells. MATLAB assigns to the variables in column order.
Check the following link for more information:
Modifying the code as
myCell = {'a';'b';'c';'d';'e'};
myTable = table(myCell)
isequal(myCell{1},vertcat(myTable.myCell))
isequal(vertcat(myCell),vertcat(myTable.myCell))
seems to work.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!