Problems with strrep: y(1)0 instead of y(10)
2 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 9 Mar 2016
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?
Answers (3)
Stephen23
on 8 Mar 2016
Edited: Stephen23
on 8 Mar 2016
>> 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 Comments
Stephen23
on 8 Mar 2016
Edited: Stephen23
on 8 Mar 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.
Guillaume
on 8 Mar 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)')
0 Comments
Star Strider
on 8 Mar 2016
I would start by replacing ‘y10’ with ‘y(10)’ and move backwards to ‘y1’ replacing ‘y(1)’ last.
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!