Accessing the values of a cell array stored in a table to make a new variable

10 visualizaciones (últimos 30 días)
Consider the following table:
mytable =
2x3 table
Var1 Var2 c
_____ _____ _______________
true true {1000x1 double}
false true {1000x1 double}
c is a cell array.
How can I create a variable based off, for example, the first element of the c vector for each row? Specifically, if c was a vector of 1000x1 vector of ones in row 1 of mytable, I would want to make a variable v with scalar value 1 in row 1 of mytable, and if c was a vector of zeroes in row 2 of mytable, I would want to make v have scalar value 0 in row 2 of mytable.
I have been trying something along the lines of mytable.v = mytable.c(:, 1) to no avail. I'm not sure of the proper way to index the cell array within the table.
Also, are cell arrays the recommended datatype for this sort of task (storing vectors/matrices in a table)? For context, c is the output of a function, and I will need to pass it to other functions using rowfun. I would rather not store each of the 1000 elements of c in a separate variable, if possible.
Thanks in advance.
  1 comentario
Isaac Liu
Isaac Liu el 7 de Abr. de 2021
I have accepted the answer below but am still interested in any comments about good practice for passing non-scalar cells (like c in this example) to rowfun
(Storing matrices/vectors in tables such that they are easy to feed to functions rowwise)

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 7 de Abr. de 2021
Var1 = [true;false];
Var2 = [true;true];
c = {ones(1000,1);zeros(1000,1)};
T = table(Var1,Var2,c)
T = 2×3 table
Var1 Var2 c _____ _____ _______________ true true {1000×1 double} false true {1000×1 double}
T.v = cellfun(@(v)v(1),T.c)
T = 2×4 table
Var1 Var2 c v _____ _____ _______________ _ true true {1000×1 double} 1 false true {1000×1 double} 0

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by