Info

此问题已关闭。 请重新打开它进行编辑或回答。

strrep with if statement use in matlab

1 次查看(过去 30 天)
azar mzory
azar mzory 2014-2-12
关闭: Matt J 2014-2-14
s= strrep(origStr, oldSubstr, newSubstr)
if str1==strrep(str,'sport','1');
else if str2==strrep(str1,'art','2');
else if str3==strrep(str2,'art_u','3');
end
end
end
what is the error ? and how to use if condition with strrep ??
if u have more replacing ?
art=2
'art_u' it's one word will be 2_u it is error
the output is art=2 but, art_u= 2_u is wrong should be 3
how to solve ????
  1 个评论
Image Analyst
Image Analyst 2014-2-13
What are the starting values of str, str1, str2, and str3? What are their values before you get to the "if" statement?

回答(1 个)

Matt J
Matt J 2014-2-12
编辑:Matt J 2014-2-12
You tell us what the error is. You haven't posted any error messages, nor given us any input data to test your code with. As a guess, though, you should probably be using the STRCMP command to compare the strings.
Incidentally, it would be more efficient to use the ELSEIF keyword, instead of "else if" all the time. Then you would require only one "end" to close the block.
if condition1
elseif condition2
elseif condition3
else
end
  2 个评论
Matt J
Matt J 2014-2-12
You should give us the code you are running and the input strings so that we can run it too. Also, say what the desired output should be.
Matt J
Matt J 2014-2-12
No, not necessarily. Neither of the following calls to strrep produces '2_u' as output. And the second one produces '3' as you say you want.
>> str='art'; strrep(str,'art_u','3')
ans =
art
>> str='art_u'; strrep(str,'art_u','3')
ans =
3

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by