フィルターのクリア

Converting table column of cell arrays to column with comma separated values

7 ビュー (過去 30 日間)
Erik J
Erik J 2022 年 4 月 6 日
コメント済み: Erik J 2022 年 4 月 6 日
I have a table where most columns are single values, but two columns are cells that contain multiple values (e.g., 3x1, 4x1, 5x1 cells). I need to convert these columns of cells to columns with comma separated values, so that {'2'} {'3'} {'4'} becomes '2,3,4'. Many rows contain all empty [] cells, and these need to be preserved.
The end goal is that this table needs to be written to a .xlsx file, where the columns with multiple values have all values in a single cell, separated by commas.

採用された回答

Stephen23
Stephen23 2022 年 4 月 6 日
A = [11;22;33;44;55];
B = {{'1'};{'2';'3';'4'};[];{'6';'7'};{'8'}};
T = table(A,B)
T = 5×2 table
A B __ ____________ 11 {1×1 cell } 22 {3×1 cell } 33 {0×0 double} 44 {2×1 cell } 55 {1×1 cell }
X = ~cellfun(@isempty,T.B);
T.B(X) = cellfun(@(c)join(c,','),T.B(X))
T = 5×2 table
A B __ ____________ 11 {'1' } 22 {'2,3,4' } 33 {0×0 double} 44 {'6,7' } 55 {'8' }

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by