Replace matched values with a cell array keeping unmatched values unchanged

1 vue (au cours des 30 derniers jours)
Shubham Gupta
Shubham Gupta le 15 Avr 2019
Commenté : Rik le 16 Avr 2019
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.

Réponse acceptée

Rik
Rik le 15 Avr 2019
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 commentaire
Rik
Rik le 16 Avr 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by