'Not equal too' statement not working

I have a program that gives the user a 5x5 grid and asks them to click on the stated coordinate. If they respond correctly, a second coordinate is generated. If not, an error message is displayed. A correct response is tested using the following code:
if UserResponse ~= RequiredCoordinate
Error.String = 'INCORRECT';
else
Error.String = '';
end
The problem is that it doesn't always work.
By not suppressing the variables I can see that the values aren't equal, however the code behaves as if they are equal, treating it like a correct response. But this problem only occurs sometimes.
I've run the code on 3 different machines, all running the same version (R2015, 1 machine runs a student license if that makes a difference) and sometimes the code runs as expected and others this problem occurs.
Can anyone suggest a solution?
Thank you.

댓글 수: 4

Stephen23
Stephen23 2015년 12월 1일
Please give us some values that show this behavior.
Thorsten
Thorsten 2015년 12월 1일
What is UserResponse? A single number, or two coordinates?
Jonathan Riley
Jonathan Riley 2015년 12월 1일
Both variables are of type 1x2 double - e.g. [5,3]
Try
~all(UserResponse==RequiredCoordinate)
instead or if the values aren't integers see the FAQ (and will still need the above if is an array of coordinates rather than single value)--

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

답변 (2개)

dpb
dpb 2015년 12월 2일

0 개 추천

As noted in the above comment prior to seeing your response...
>> x=[5,3]; y=[5,2];
>> all(x==y)
ans =
0
>> y(2)=3;
>> all(x==y)
ans =
1
>>
Again, note the caution re: floating point comparisons if the values aren't integer; you'll have to use "fuzzy" comparison for a robust test.
James Tursa
James Tursa 2015년 12월 2일
편집: James Tursa 2015년 12월 2일

0 개 추천

Another method using the function isequal, which is useful for arrays:
if ~isequal(UserResponse,RequiredCoordinate)

카테고리

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

질문:

2015년 12월 1일

편집:

2015년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by