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

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.

 Accepted 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.

2 Comments

Thank you so much, I checked my other functions I had and when I meant to assign something to stats(2) I was just assigning it to stats.
As always, my pleasure!
(I’ve done something like that as well from time to time.)
.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!