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일
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에 대해 자세히 알아보기

질문:

2012년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by