У меня есть функция, которая принимает необязательные аргументы как пары имя/значение.
function example(varargin)
% Lots of set up stuff
vargs = varargin;
nargs = length(vargs);
names = vargs(1:2:nargs);
values = vargs(2:2:nargs);
validnames = {'foo', 'bar', 'baz'};
for name = names
validatestring(name{:}, validnames);
end
% Do something ...
foo = strmatch('foo', names);
disp(values(foo))
end
example('foo', 1:10, 'bar', 'qwerty')
Похоже, что при извлечении соответствующих значений прилагается много усилий (и он по-прежнему не является особенно надежным снова неправильно указанными входами). Есть ли лучший способ обработки этих пар имя/значение? Есть ли вспомогательные функции, которые поставляются с MATLAB, чтобы помочь?