Putting 2 variables in an if loop
조회 수: 5 (최근 30일)
이전 댓글 표시
Hey I'm trying to build an if loop and instead of repeating the same line over and over again I wanted to see if there is anyway of puttigs on variable equal to two values (in my case names). I tried putting the names in brackets but it gives me an answer = logical 0 whoch I do not want I want only the fprintf statement to appear after entering the variable value. Please let me know if you have a solution

댓글 수: 0
답변 (1개)
DGM
2021년 4월 22일
Doing direct comparison with strings isn't really going to work that way; certainly not with that syntax. A string is just a character vector. If you try to compare two vectors of unequal length for equality, you'll get an error. If you do this:
D = ['A','B','C'];
Then that's just going to concatenate them. D is 'ABC'.
Use strcmp(), strcmpi(), ismember() etc for handling string comparison. If you're going to test a lot of cases, you can just avoid all that and do this.
switch mystring
case 'this'
% do a thing
case 'that'
% do a different thing
case {'another','thing'}
% do something else
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!