Created
February 25, 2014 13:39
-
-
Save myfavouritekk/9208866 to your computer and use it in GitHub Desktop.
Matlab: Variable-length input argument (varargin) list checking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% checking minimum arguments requried | |
minargs = 3; | |
maxargs = inf; | |
n_exargs = length(varargin); | |
try | |
narginchk(minargs, maxargs); | |
catch err | |
if strcmp(err.identifier,'MATLAB:narginchk:notEnoughInputs') | |
error('batchconvert:notEnoughInputs',... | |
'Not enough inputs, at least %d arguments are needed. Including indir, outdir and function handle.',minargs); | |
else | |
rethrow(err); | |
end | |
end | |
%% default parameters | |
opts = struct('samebasename', true, 'sameextension', true,... | |
'inputextension', '', 'outputextension', '',... | |
'startwith', '', 'endwith', '',... | |
'verbose', false); | |
optnames = fieldnames(opts); % case insensitive | |
%% override default parameters | |
if rem(n_exargs,2) % odd number of name/value | |
error('batchconvert:notParameterValuePairs',... | |
'Arguments after first %d should be parameter name/value pairs.',... | |
minargs); | |
end | |
for pair = reshape(varargin,2,[]) | |
inoptname = lower(pair{1}); % making it case insensitive | |
if validatestring(inoptname, optnames) | |
opts.(inoptname) = pair{2}; | |
else | |
error('batchconvert:invalidparameter','%s is not a valid parameter.',... | |
inoptname); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment