Parsing a multi cell array into character vectors to place in table

1 回表示 (過去 30 日間)
Calathea Ornata
Calathea Ornata 2020 年 5 月 19 日
編集済み: Calathea Ornata 2021 年 3 月 26 日
Hello,
I need help with logic regarding the parsing of a multi cell array into specific character vectors to later populate in a table. For example I have a 3x1 cell array that is transposed.
List = {'10_ab_cd_ef_gh', '100_ij_kl_mn_op', '1000_qr_st_uv_wx'}'
I would like to parse out each cell container by "_" into its own cell and create a new 3x5 cell array. For example for {'10_ab_cd_ef_gh'}, I'd like to have a cell array with 10 in cell {1,1}{1,2}, ab in cell {1,1}{1,2} and so on.
I'm looking for a final output of that would have:
{10} {ab} {cd} {ef} {gh}
{100} {ij} {kl} {mn} {op}
{1000} {qr} {st} {uv} {wx}
And finally new variables that would extract all data from the columns for input into a table. For example: 'num' 'lettes1' 'letters2' 'letters3' 'letters4' as variable names.
I appreciate any help in advance!! Thanks

採用された回答

the cyclist
the cyclist 2020 年 5 月 19 日
編集済み: the cyclist 2020 年 5 月 19 日
Here is a method, using regexp to identify where the separators are, and then for loops to use the separators as the "fenceposts" to identify the intervening characters.
FullList = {'10_ab_cd_ef_gh', '100_ij_kl_mn_op', '1000_qr_st_uv_wx'}';
sepIndices = regexp(FullList,'_');
height = size(FullList,1);
width = numel(sepIndices{1}) + 1;
C = cell(height,width);
for nh = 1:height
fenceposts = [0 sepIndices{nh} length(FullList{nh}) + 1];
for nw = 1:width
C{nh,nw} = FullList{nh}(fenceposts(nw)+1:fenceposts(nw+1)-1);
end
end
T = cell2table(C,'VariableNames',{'num','lettes1','lettes2','lettes3','lettes4'});

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by