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.

 採用された回答

Jan
Jan 2012 年 12 月 18 日
編集済み: Jan 2012 年 12 月 18 日

0 投票

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 件のコメント

Mitra
Mitra 2012 年 12 月 18 日
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 ExchangeCharacters and Strings についてさらに検索

質問済み:

2012 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by