필터 지우기
필터 지우기

Check for optional inputs in a function using exist

조회 수: 39 (최근 30일)
Haris K.
Haris K. 2020년 10월 28일
답변: Walter Roberson 2020년 10월 28일
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

채택된 답변

Walter Roberson
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개)

카테고리

Help CenterFile Exchange에서 Test Model Components에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by