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

11 ビュー (過去 30 日間)
Isaac Liu
Isaac Liu 2021 年 4 月 6 日
コメント済み: Isaac Liu 2021 年 4 月 7 日
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 件のコメント
Isaac Liu
Isaac Liu 2021 年 4 月 7 日
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)

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2021 年 4 月 7 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by