Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

strrep with if statement use in matlab

1 Ansicht (letzte 30 Tage)
azar mzory
azar mzory am 12 Feb. 2014
Geschlossen: Matt J am 14 Feb. 2014
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 Kommentar
Image Analyst
Image Analyst am 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?

Antworten (1)

Matt J
Matt J am 12 Feb. 2014
Bearbeitet: Matt J am 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 Kommentare
Matt J
Matt J am 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
Matt J am 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

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by