Indexing a cell array in a table

22 次查看(过去 30 天)
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?

采纳的回答

Debarati Banerjee
Debarati Banerjee 2015-4-13
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by