Info
This question is closed. Reopen it to edit or answer.
strrep with if statement use in matlab
1 view (last 30 days)
Show older comments
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 Comment
Image Analyst
on 13 Feb 2014
What are the starting values of str, str1, str2, and str3? What are their values before you get to the "if" statement?
Answers (1)
Matt J
on 12 Feb 2014
Edited: Matt J
on 12 Feb 2014
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 Comments
Matt J
on 12 Feb 2014
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
on 12 Feb 2014
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
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!