Borrar filtros
Borrar filtros

Problems with strrep: y(1)0 instead of y(10)

3 visualizaciones (últimos 30 días)
Marius
Marius el 8 de Mzo. de 2016
Editada: Stephen23 el 9 de Mzo. de 2016
I'm having a problem replacing some strings with strrep.
I have to replace some strings y1,y2,y3,y4 etc with y(1),y(2),y(3),y(4)and so on. This works fine until y(9), but for higher numbers it then makes for example for y10 = y(1)0 instead of y(10). What is the problem?
if true
%Create the strings for conversion table
table=cell(length(y_sym),2);
j = 1;
while j<=length(y_sym)
table{j,1}= strcat('y',num2str(j));
table{j,2}= strcat('y(',num2str(j),')');
j=j+1;
end
j=1;
while j<=length(y_sym)
str=strvcat(dy_sym(j));
if ~ischar(str)
str=char(str);
end
if any(size(str)~=1)
str=cellstr(str)
end
for i=1:size(table,1)
str_old = table{i,1};
str_new = table{i,2}
str=strrep(str,str_old,str_new)
end
if iscellstr(str)
fprintf(fid,repmat('%s\n',1,length(str)),str{:});
else
fprintf(fid,'%s\n',str);
end
j=j+1;
end
ok=true;
fclose(fid);
end
if nargout varargout{1}=ok; else if ok fprintf('Wrote %s successfully.\n',mfile_name); end
  2 comentarios
Stephen23
Stephen23 el 9 de Mzo. de 2016
Marius's "Answer" moved here:
Thanks all it worked! Can someone tell me how i can write comments with fprintf? so that I have
%Comment here
in the file i create with fprintf?
Stephen23
Stephen23 el 9 de Mzo. de 2016
Editada: Stephen23 el 9 de Mzo. de 2016
Here are two ways:
>> fprintf('%s\n','% Comment here')
% Comment here
>> fprintf('%%%s\n','Comment here')
% Comment here
These are explained in the fprintf documentation.
PS: It is considered polite on this forum to accept the answer the helped resolve your question.

Iniciar sesión para comentar.

Respuestas (3)

Stephen23
Stephen23 el 8 de Mzo. de 2016
Editada: Stephen23 el 8 de Mzo. de 2016
Instead of writing your own parser you should simply use regexprep:
>> str = 'abc y1 y2 mno y10 y11 xyz'
str =
abc y1 y2 mno y10 y11 xyz
>> regexprep(str,'y(\d+)','y($1)')
ans =
abc y(1) y(2) mno y(10) y(11) xyz
For learning about Regular Expressions you might like to try my FEX submission too:
  3 comentarios
Stephen23
Stephen23 el 8 de Mzo. de 2016
Editada: Stephen23 el 8 de Mzo. de 2016
EDIT: Marius originally wrote a comment claiming that regeprep did not work. My response below.
Interesting... my answer shows that regexprep can do exactly what you want.
Please upload y_sym into a comment.
BTW, the problem is that you forgot to consider that y1 is a match before y10 is. My advice would be to use regexprep, which by default uses a greedy match and will always match the longer one correctly (when it is used properly). This is going to be much faster and more robust than trying to parse this yourself.
Stephen23
Stephen23 el 8 de Mzo. de 2016
@Marius: my pleasure! You can also accept my answer if it helped you.

Iniciar sesión para comentar.


Guillaume
Guillaume el 8 de Mzo. de 2016
First, learn to use matlab's debugger. Had you stepped through the code, you'd have found the bug quickly.
You test the following search strings in order: 'y1', 'y2', ..., 'y10'. Therefore, in a string 'xxy10xx', your first iteration will find 'y1' and replace it with 'y(1)', and your string becomes 'xxy1(0)xx'. When it comes to searching 'y10', it's not to be found anymore. The fix is simple, search for the longer strings first, so reverse the order of your loop.
Better yet, no need for a loop or this building of search and replacement string, use a regular expression:
str = regexprep(str, 'y(\d+)', 'y\($1)')

Star Strider
Star Strider el 8 de Mzo. de 2016
I would start by replacing ‘y10’ with ‘y(10)’ and move backwards to ‘y1’ replacing ‘y(1)’ last.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by