indexing multiple occurrence in a string using regexprep

Hi!
I have a string where Plot_Num occurs multiple times. Is there any way to use regexprep to find all occurrence of Plot_Num and replace them with Plot_Num 1, Plot_Num 2, ... in the order they are found.
Thanks.

 Accepted Answer

Jan
Jan on 18 Dec 2012
Edited: Jan on 18 Dec 2012
S = 'asdhaskh dPlot_Numlaksdlkab sPlot_Numasd ';
num = length(strfind(S, 'Plot_Num));
T = strrep(S, 'Plot_Num', 'Plot_Num %d');
R = sprintf(T, 1:num);

1 Comment

Ok. I tried your answer and it looks like it works on a string where there is no %. The string that I have is actually an html file written in matlab so every line starts with %. I can take % out and then add it (I think) but the problem is that in this file I have references to some charts stored in different directories thus end up using for example charts\2hr\. The problem is that the sprintf stops printing when it reaches \.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 18 Dec 2012

Community Treasure Hunt

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

Start Hunting!