Replace matched values with a cell array keeping unmatched values unchanged
1 次查看(过去 30 天)
显示 更早的评论
I have a string let's say
A = '[0, 40, 50, 60, 80, 100, 140, 160, 200, 300]';
and another char array, say
B = '48 43 533 6320 840 43 13 13 24 49';
I want to replace numeric values in "A" by numeric values in "B". I tried to match numerical values in B using:
B_num = regexp(A,'\d*','match');
then I tried to replace the numeric values in B without chaging non-numeric values in A using :
A_update = regexprep(A,'\d*',[B_num{:}])
which obivously didn't give the desired results. I know my mistakes but I am unable to really think how I can achieve the desired result.
0 个评论
采纳的回答
Rik
2019-4-15
This code should help
A = '[0, 40, 50, 60, 80, 100, 140, 160, 200, 300]';
B = '48 43 533 6320 840 43 13 13 24 49';
B_num = regexp(B,'\d*','match');
A_pad = regexp(A,'\d*','split');
B_num{end+1}=[];%extend to match size of pad
C=[A_pad(:) B_num(:)]';%put the padding and values back together again
C=C(:)';C(end)=[];%make linear and remove empty last element
C=cell2mat(C);%convert back to char array
But please don't use this in a statement with eval...
1 个评论
Rik
2019-4-16
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!