Replace matched values with a cell array keeping unmatched values unchanged

1 回表示 (過去 30 日間)
Shubham Gupta
Shubham Gupta 2019 年 4 月 15 日
コメント済み: Rik 2019 年 4 月 16 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by