Why does the || absolutely not work?

Hello,
My Code-Fragment is:
location = 'nswe';
if strfind(location,'o') > 0 || strfind(location,'e') > 0
disp('true');
end
when I try to execute it, the message
Operands to the || and && operators must be convertible to logical scalar values.
appears. I have no clue how on earth I'm supposed to make that work. can anybody help me? :/

댓글 수: 1

Because strfind returns empty
>> strfind(location,'o')
ans =
[]

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2014년 12월 23일
편집: John D'Errico 2014년 12월 23일

4 개 추천

When something like this happens, take your expression apart. Look at the pieces. Then think about what MATLAB is telling you.
location = 'nswe';
strfind(location,'o') > 0
ans =
[]
strfind(location,'e') > 0
ans =
1
So the first fragment returns empty, the second returns true.
What does the operator do there?
[] || 1
Operands to the || and && operators must be convertible to logical scalar values.
Is an empty result a scalar, logical value? No. So you need to think about how to better write that test to not fail.
For example, since you know that strfind may return zero, one, or more than one solution depending on the string, you need to write code that will succeed in any case. Of course, I'm not terribly sure why you are testing to see if 'o' falls in the string 'nswe', but maybe you have a valid reason.
(I could show you a better way to write that test, but I don't know what strings you might have as possibilities. Only you know your real problem.)

댓글 수: 4

Chris
Chris 2014년 12월 23일
I just added the line "location = 'nswe'" to show what this string may contain - it is supposed to be north/south/west/east, and since I'm german and have a bad memory, also Ost instead of east...
but how do I check for an empty array? I mean, something like ">0" should return false, shouldn't it?
Sean de Wolski
Sean de Wolski 2014년 12월 23일
isempty
An alternate approach
switch lower( location )
case {'east','ost','öster'}
...
case {'south','süd','syd'}
...
otherwise
end
John D'Errico
John D'Errico 2014년 12월 23일
편집: John D'Errico 2014년 12월 23일
Empty is not > 0, nor is it less than or equal to zero. All it can be is empty. I'll admit that you can argue this either way, but that is how the > operator works.
So you might do this instead:
location = 'nswe';
if any([strfind(location,'o') > 0 ,strfind(location,'e') > 0])
disp('true');
end
which caters to the empty case as well as a true result by either subpart. This also works:
location = 'nswe';
if ~isempty(strfind(location,'o')) || ~isempty(strfind(location,'e'))
disp('true');
end
There are always many ways to solve a problem like this if you look. Sean had a nice alternative with ismember.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2014년 12월 23일

2 개 추천

How about just using ismember
if any(ismember('oe',location))
disp(true)
end

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2014년 12월 23일

편집:

2014년 12월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by