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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by