Indexing a cell array in a table

조회 수: 24 (최근 30일)
Michael
Michael 2015년 4월 9일
답변: Debarati Banerjee 2015년 4월 13일
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에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by