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.

 Réponse acceptée

Jan
Jan le 18 Déc 2012
Modifié(e) : Jan le 18 Déc 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 commentaire

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by