使用斷言

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};