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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

the cyclist
the cyclist am 19 Mai 2020
Bearbeitet: the cyclist am 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'});

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by