Check for optional inputs in a function using exist
30 次查看(过去 30 天)
显示 更早的评论
Hi everyone. Is it wrong, prone to mistakes, or slower to check the existance of optional arguments in a function using exist()? For the purposes of the illustration, assume I have 2 required and 1 optional inputs named r1,r2 and op1, respectively.
function y = mafun(r1,r2,op1)
if ~(exist('op1', 'var')==1)
op1 = 'smthn';
end
%do smthn with r1,r2 and op1 (when needed)
end
1 个评论
采纳的回答
Walter Roberson
2020-10-28
I suspect that it is faster to check nargin.
Note: starting R2019b, it is possible to use an "arguments" block to do automatic default values.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Test Model Components 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!