フィルターのクリア

Trouble concatenating a cell.

1 回表示 (過去 30 日間)
Paul Safier
Paul Safier 2023 年 9 月 21 日
コメント済み: Voss 2023 年 9 月 21 日
I want to concatenate the cell in the image such that rows 1,2 and10 are expanded, i.e. rows 1 and 2 each become 4 rows. Row 10 should become 2 rows. Thus, when done, the cell will be size [20 4] and not [13 4].
Ultimately I want this data into a table to write to disk as a csv. I plan to use writetable...
I'm not having any luck with cell concatenation operations gleaned online. Any suggestions?
Thanks.

採用された回答

Voss
Voss 2023 年 9 月 21 日
編集済み: Voss 2023 年 9 月 21 日
Here's one way to make a table out of it:
% a cell array like yours:
tst = { ...
"n"+string(randi(10,4,1)),zeros(4,1),rand(4,1),rand(4,1); ...
"n"+string(randi(10,4,1)),zeros(4,1),rand(4,1),rand(4,1); ...
"n1",0,-3000,-3000; ...
"n2",0,-4000,-4300; ...
"n"+string(randi(10,2,1)),zeros(2,1),rand(2,1),rand(2,1); ...
"n3",0,-4400,-4500; ...
}
tst = 6×4 cell array
{4×1 string} {4×1 double} {4×1 double} {4×1 double} {4×1 string} {4×1 double} {4×1 double} {4×1 double} {["n1" ]} {[ 0]} {[ -3000]} {[ -3000]} {["n2" ]} {[ 0]} {[ -4000]} {[ -4300]} {2×1 string} {2×1 double} {2×1 double} {2×1 double} {["n3" ]} {[ 0]} {[ -4400]} {[ -4500]}
% vertcat each column, store in args cell to be sent to table:
N = size(tst,2);
args = cell(1,N);
for ii = 1:N
args{ii} = vertcat(tst{:,ii});
end
T = table(args{:})
T = 13×4 table
Var1 Var2 Var3 Var4 _____ ____ ________ _______ "n7" 0 0.44091 0.53584 "n1" 0 0.9128 0.38251 "n10" 0 0.10853 0.80617 "n8" 0 0.1388 0.16147 "n2" 0 0.59 0.83124 "n5" 0 0.3631 0.24028 "n10" 0 0.068072 0.79592 "n6" 0 0.88154 0.5531 "n1" 0 -3000 -3000 "n2" 0 -4000 -4300 "n10" 0 0.22986 0.98944 "n10" 0 0.063354 0.11196 "n3" 0 -4400 -4500
  2 件のコメント
Paul Safier
Paul Safier 2023 年 9 月 21 日
That works perfectly! Thanks @Voss !!
Voss
Voss 2023 年 9 月 21 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by