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.

 采纳的回答

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 个评论

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 \.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by