Replace matched values with a cell array keeping unmatched values unchanged
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Rik
on 15 Apr 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 Comment
Rik
on 16 Apr 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.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!