GETOPTION Extract one option from an option list ['Name',value,...] OPTVAL=GETOPTION(ARGS,OPTNAME) Return the value of the desired option from the argument list and send an exception if it is absent. Option names are case insensitive OPTLIST: Option list (cell array or structure) OPTNAME: Name of the desired option OPTVAL=GETOPTION(ARGS,OPTNAME,OPTDEFAULT) Return a default value if the option is absent OPTDEFAULT: Default value for the option [OPTVAL,NEWARGS]=GETOPTION(...) Returns remaining options after removing the processed ones See also GETFLAG
0001 function [optval,opts] = getoption(opts,optname,defval) 0002 %GETOPTION Extract one option from an option list ['Name',value,...] 0003 % 0004 %OPTVAL=GETOPTION(ARGS,OPTNAME) 0005 % Return the value of the desired option from the argument list and 0006 % send an exception if it is absent. Option names are case insensitive 0007 % OPTLIST: Option list (cell array or structure) 0008 % OPTNAME: Name of the desired option 0009 % 0010 % 0011 %OPTVAL=GETOPTION(ARGS,OPTNAME,OPTDEFAULT) 0012 % Return a default value if the option is absent 0013 % OPTDEFAULT: Default value for the option 0014 % 0015 %[OPTVAL,NEWARGS]=GETOPTION(...) 0016 % Returns remaining options after removing the processed ones 0017 % 0018 %See also GETFLAG 0019 0020 if iscell(opts) 0021 ok=[strcmpi(optname,opts(1:end-1)) false]; %option name cannot be the last argument 0022 if any(ok) 0023 okval=circshift(ok,[0,1]); 0024 defval=opts{find(okval,1,'last')}; 0025 opts(ok|okval)=[]; 0026 end 0027 elseif isstruct(opts) 0028 if isfield(opts,optname) 0029 defval=opts.(optname); 0030 opts=rmfield(opts,optname); 0031 end 0032 end 0033 try 0034 optval=defval; 0035 catch 0036 error('getoption:missing','Option "%s" is missing',optname); 0037 end 0038 end