Modify Strings in cell array according to pattern
显示 更早的评论
I have to cell arrays A & B
A = ['C1 H2 O1 N1 S1';
[];
'C5 H2 O0 N1 S0';
'C2 H6 O0 N1 S1';
'C2 H6 O2 N1 S0';
[];
'C3 H10 O1 N1 S0';
'C6 H5 O2 N2 S1';
'C8 H9 O4 N0 S0';
[];
'C9 H13 O3 N0 S0';
'C10 H17 O2 N0 S0';
'C11 H21 O1 N0 S0';
'C12 H25 O0 N0 S0'];
and
B = ['C5H9O2+'
'C3H5O4'
'C3H7O5']
and I would like to have B in the same pattern as A so that (C* H* O* N* S*)
and deleting the +
B[1] = 'C5 H9 O2 NO S0'
kinda tried with regexprep() but didn't get anything by now.....
采纳的回答
更多回答(1 个)
Allen
2020-1-16
I am sure that there is a much more elegant method using just regular expressions, but the following should give you what you are looking for if your input cell-array is similar to the example provided.
B = {'C5H9O2+'
'C3H5O4'
'C3H7O5'};
I = {'C','H','O','N','S'};
% Looks for 'C', 'H', 'O', 'N',and 'S' and when not present in a cell-array element, append the
% missing charater followed by a '0' to the cell-array element.
for i=1:length(I)
idx = ~contains(B,I{i});
B(idx) = append(B(idx),I{i},'0');
end
% Use regular expression to replace all non-alphanumeric or underscore characters, then find
% matching tokens consisting of 'C', 'H', 'O', 'N', and 'S' each followed by any number of digits
% and recombine them as a single space-delimited string.
regexprep(B,{'\W','(C\d*)(H\d*)(O\d*)(N\d*)(S\d*)'},{'','$1 $2 $3 $4 $5'})
3 个评论
Martin Muehlegger
2020-1-17
Allen
2020-1-17
What version of MATLAB are you using? Also, you appear to be defining B as a non-cell-array instead of a cell-array. That might also be part of the problem.
Martin Muehlegger
2020-1-17
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!