MATLAB: How to replace a single word in a string with multiple row string

regexprepregular expressionstrrep

Hello,
I am trying to replace a single word/token in a string variable with a string containing multiple rows of words. I get the following error using regexprep function :
Error using regexprep The 'REPLACE' input must be a one-dimensional array of char or cell arrays of strings.
I tried using strrep function as well. I get the following error for it :
Error using strrep Input strings must have one row.
A = strrep(B,'Keyword',C);
A = regexprep(B,'Keyword',C);
I am trying to replace Keyword in String B with C. C has multiple rows of words in it.
I searched the forum for similar questions. Did not find any.
How do i prevent this error? Please help.

Best Answer

This is possibly what you want:
B = 'Some string with a Keyword in it';
C = {'foo', 'bar', 'baz'};
A = cellfun(@(rep) strrep(B, 'Keyword', rep), C, 'UniformOutput', false)
If your C is a 2D char array and you also want a 2D char array as an output
B = 'Some string with a Keyword in it';
C = ['foo';'bar';'baz'];
A = cell2mat(cellfun(@(rep) strrep(B, 'Keyword', rep), num2cell(C, 2), 'UniformOutput', false))