使用断言

Matlab 允许一些非常微不足道的错误无声地进行,这可能导致在运行的后期很长时间内引发错误 - 使调试变得困难。如果你假设某些变量,请对其进行验证

function out1 = get_cell_value_at_index(scalar1,cell2)
assert(isscalar(scalar1),'1st input must be a scalar')
assert(iscell(cell2),'2nd input must be a cell array')

assert(numel(cell2) >= scalar1),'2nd input must have more elements than the value of the 1st input')
assert(~isempty(cell2{scalar1}),'2nd input at location is empty')

out1 = cell2{scalar1};