Filter löschen
Filter löschen

How to replace exact char in a cell array from another reference cell array

2 Ansichten (letzte 30 Tage)
Given the example below
A = {'x1', 'x10', 'x1:x10'};
B = {'x1','x10'};
C = {'Pierre', 'Paul'};
I would like to replace in A the exact string from B by its respective value from C. I would like to obtain:
A={'Pierre', 'Paul', 'Pierre:Paul'}
I would like to minimize the number of loops necessary.
This doest not work and produce: {'Pierre'} {'Pierre0'} {'Pierre:Pierre0'}
I tried few regexprep and strrep without success. What is the proper way? Thank you

Akzeptierte Antwort

Stephen23 am 5 Apr. 2024
Bearbeitet: Stephen23 am 5 Apr. 2024
This is difficult because 'x1' is also a substring of 'x10'. But making a few assumptions it might be possible:
A = {'x1', 'x10', 'x1:x10'};
B = {'x1','x10'};
C = {'Pierre', 'Paul'};
R = strcat(B,'\>');
D = regexprep(A,R,C)
D = 1x3 cell array
{'Pierre'} {'Paul'} {'Pierre:Paul'}

Weitere Antworten (0)


Mehr zu Function Creation finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by