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.

채택된 답변

Rik
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
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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by