How do I insert a 1x1 cell array consisting of one vector into a table?

11 views (last 30 days)
I have the following code block to set up my problem:
test_table = table('Size',[2,1],'VariableTypes',"cell",'VariableNames',["Coordinates"]);
test_array = [2,2,2,2,2]; % single row vector
% Try assigning test_array to a slot in the table
test_table(1,1) = {test_array};
Error using ()
Conversion to cell from double is not possible.
Notice that the code cell above throws the following error "Conversion to cell from double is not possible." However, if I change the value of test_array to a matrix, and try the assignment again...
test_array = [2,2,2,2,2;3,3,3,3,3]; % matrix
% Try assigning test_array to a slot in the table
test_table(1,1) = {test_array}
...things go off without a problem. First question is, why does matlab treat the assignment differently between a vector and a matrix? Second question is, how do I make the assignment in the first code block (assigning a 1x1 cell array consisting of a single row vector into a slot in a table) work?

Accepted Answer

Stephen23
Stephen23 on 3 Aug 2022
Edited: Stephen23 on 3 Aug 2022
So far no one adressed your questions, nor explained the cause of your problem: the problem comes from the fact that you are using parentheses (to refer to the table itself) as opposed to using curly braces (to refer to the table content).
Q1: I don't know. This question asks about (possibly undocumented) behavior of MATLAB implicitly coercing a cell array into table, because of the fact that you used parentheses referring to a (subset of the) table instead of curly braces to refer to the content of the table. This means your code basically tries to turn a cell array into a table, which is not a well-defined coersion. You could spend some time trying to find out under what conditions that implicit coersion will occur (or not), but the much simpler solution is to use the correct curly braces.
Q2: very easily by using the correct curly braces to refer to the table content:
Here is your code fixed, the only change is to use curly braces to refer to the table content:
test_table = table('Size',[2,1],'VariableTypes',"cell",'VariableNames',["Coordinates"])
test_table = 2×1 table
Coordinates ____________ {0×0 double} {0×0 double}
test_array = [2,2,2,2,2;3,3,3,3,3]; % matrix
test_table{1,1} = {test_array}
test_table = 2×1 table
Coordinates ____________ {2×5 double} {0×0 double}
test_array = [2,2,2,2,2]; % single row vector
test_table{1,1} = {test_array}
test_table = 2×1 table
Coordinates _____________ {[2 2 2 2 2]} {0×0 double }
Note1: you can certainly use parentheses to refer to the table itself, and use this to replace parts of the table with another table. That would work, and might be useful in some situations.
Note2: This use of parentheses vs curly braces also applies to other container types, e.g. cell arrays & strings.
  2 Comments
Stephen23
Stephen23 on 3 Aug 2022
"I was not aware one could use curly braces to index a table, whether it be referring to a subset of the table or referring to the contents of the table."
Curly braces always refer to the content of the table.
Parentheses always refer to (a subtable of) the table itself.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 3 Aug 2022
coordinates = [2,2,2,2,2]'; % single row vector
T = table(coordinates)
T = 5×1 table
coordinates ___________ 2 2 2 2 2

Community Treasure Hunt

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

Start Hunting!