Inconsistent type following cell2table

6 visualizaciones (últimos 30 días)
JZ
JZ el 3 de Mayo de 2022
Editada: Stephen23 el 3 de Mayo de 2022
Converting cell array with cell2table results in inconsistent conversion of arrays depending on (as far as I can tell) other members of the table column. Unfortunately this is causing issues for me, is there any simple fix that does not require changing inputs on a case-by-case basis?
MRE:
test = {[1, 2], [nan]; [nan], [nan]; [nan], [nan]};
test_table = cell2table(test);
test_table{2, 1}{1}; % okay (returns nan)
test_table{2, 2}{1}; % error: Brace indexing is not supported for variables of this type.
  2 comentarios
Stephen23
Stephen23 el 3 de Mayo de 2022
"Converting cell array with cell2table results in inconsistent conversion of arrays depending on (as far as I can tell) other members of the table column."
The CELL2TABLE documentation states here how columns are handled depending on their array sizes:
  • "If the contents of the cells in a column of C have compatible sizes and types, then the corresponding table variable is the vertical concatenation of those contents into an array."
  • "If the contents of the cells in a column have different sizes and types, then the corresponding table variable is a cell array."
Your 1st column will be a cell array (because [1,2] cannot be concatenated vertically with NaN), and because scalar NaNs can be concatenated the 2nd column will be numeric. Lets now check with your example data:
C = {[1,2],nan;nan,nan;nan,nan}
C = 3×2 cell array
{[1 2]} {[NaN]} {[NaN]} {[NaN]} {[NaN]} {[NaN]}
T = cell2table(C)
T = 3×2 table
C1 C2 _______ ___ {[1 2]} NaN {[NaN]} NaN {[NaN]} NaN
So far eveything seems to be working exactly as documented and as expected.
What do you expect the 1st column to be?
What do you expect the 2nd column to be?
JZ
JZ el 3 de Mayo de 2022
Thanks for your answer and pointing me to the relevant part of the documentation, I missed that.
Good to know it is working as expected, although I think this is a design decision that should be revisited. I do not think it is good practice to have a function that casts the same datatype to two different datatypes within the same table. I would have no particular expectation for them to be [nan] or nan after conversion, but I would expect consistency.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 3 de Mayo de 2022
Use parentheses, not curly braces, to index into a vector within a cell —
test = {[1, 2], [nan]; [nan], [nan]; [nan], [nan]};
test_table = cell2table(test)
test_table = 3×2 table
test1 test2 _______ _____ {[1 2]} NaN {[NaN]} NaN {[NaN]} NaN
test_table{2, 1}(1) % okay (returns nan)
ans = 1×1 cell array
{[NaN]}
test_table{2, 2}(1) % error: Brace indexing is not supported for variables of this type.
ans = NaN
test_table{1,1}
ans = 1×1 cell array
{[1 2]}
test_table{1,1}{1}(1)
ans = 1
test_table{1,1}{1}(2)
ans = 2
The indexing can get a bit complicated.
.
  3 comentarios
Star Strider
Star Strider el 3 de Mayo de 2022
As always, my pleasure!
Stephen23
Stephen23 el 3 de Mayo de 2022
Editada: Stephen23 el 3 de Mayo de 2022
@JZ: it seems that you could avoid the different column conversions of CELL2TABLE by... not using CELL2TABLE. Why not just access the cell array itself? Then your indexing would always be the same, no errors, correct data.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by