You can use the numel
function to obtain the number of elements in the array. Then, use an if
statement to check if the index is within the bounds of the array. Here is an example:
main.m143 chars9 lines
In this example, the index 6
is out of bounds, so the output would be 'Index is out of bounds'
. If the index was 3
, the output would be 'Index is within bounds'
.
gistlibby LogSnag