MATLAB: Error: Index exceeds the number of array elements (1).

errorMATLABmatlab function

I have an array variable stats defined as
stats = [1,1,0];
and when running this function
function gameOver = overCheck(stats)
if stats(1) == 0
gameOver = true;
elseif stats(2) == 0
gameOver = true;
else
gameOver = false;
end
end
I get the error "Index exceeds the number of array elements (1)."
"Error in game2>overCheck (line 73)
elseif stats(2) == 0"
I'm wondering why it this is happening? When accessing stats(2) in other functions it works fine.

Best Answer

The only possibility that comes to mind is that when you call ‘gameOver’, ‘stats’ is a scalar. It may have been redefined before then.
The function works correctly when I run it with the ‘stats’ vector you provided.