Making Matlab throw an error on index out of bounds
Show older comments
I'm curious if it's possible to force Matlab to throw an error when an an array with predefined size is indexed out of bounds. For example, I'd like the following code to throw an error instead of simply making "i" 11 elements long:
function i = test()
%#codegen
i=zeros(10,1);
for j=1:11
i(j) = 1;
end
end
Accepted Answer
More Answers (2)
dpb
on 17 Oct 2014
AFAIK, automagic reallocation is built in so deeply there's no way to avoid it. Best you can do is sotoo
assert(j>length(i), 'Array bound exceeded.')
in the loop before the access. This is bound to have highly detrimental effect on run time (as does bounds checking in compiled languages as well when turned on).
David
on 17 Oct 2014
0 votes
Categories
Find more on Function Definition in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!