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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

the cyclist
the cyclist le 19 Mai 2020
Modifié(e) : the cyclist le 19 Mai 2020
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'});

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by