Efficient handling of many optional arguments
조회 수: 6 (최근 30일)
이전 댓글 표시
I would like to allow the user to pass a string and a variable into my function to override defaults. e.g. native function 'plot' can take 'color','markersize' etc.
I currently use this function:
function [ val ] = getVarArg( str )
%GETVARARG Retrieves value of optional argument
args = evalin('caller','varargin');
idx = strcmpi(args,str);
if sum(idx)>0
val = args{find(idx)+1};
end
end
I call it inside the function I am passing on option argument to like so
try optionalValue = getVarArg('optionalVar');
catch
optionalVar = defaultValue
end
but it feels a bit clunky. Is there a more elegant way to do this?
댓글 수: 3
jgg
2016년 1월 26일
When you've solved your problem, answer your own question to let us know how you solved it!
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!