Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
strrep with if statement use in matlab
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Image Analyst
le 13 Fév 2014
What are the starting values of str, str1, str2, and str3? What are their values before you get to the "if" statement?
Réponses (1)
Matt J
le 12 Fév 2014
Modifié(e) : Matt J
le 12 Fév 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 commentaires
Matt J
le 12 Fév 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
le 12 Fév 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
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!