Efficient handling of many optional arguments

조회 수: 6 (최근 30일)
Rall Walsh
Rall Walsh 2016년 1월 26일
댓글: jgg 2016년 1월 26일
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
Rall Walsh
Rall Walsh 2016년 1월 26일
Nope, I hadn't encountered that one before. Thanks a lot, looks like it will be useful
jgg
jgg 2016년 1월 26일
When you've solved your problem, answer your own question to let us know how you solved it!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by