-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvarargparse.m
More file actions
65 lines (58 loc) · 1.98 KB
/
varargparse.m
File metadata and controls
65 lines (58 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function [out] = varargparse(args, params, defaults)
%VARARGPARSE Parse varargin given expected parameters and defaults
%
% VARARGPARSE(ARGS, PARAMS, DEFAULTS) parses ARGS given expected parameters
% PARAMS and default values DEFAULTS. Param-value pairs that appear in ARGS
% but not in PARAMS cause errors. PARAMS and DEFAULTS are both cell arrays
% where a parameter in PARAMS has a default value in the same position in
% DEFAULTS.
%
% Due to the way MATLAB resolves variable names, if any of the names in PARAMS
% is a built in function, you may run into problems. Resolve this by adding
% initializations before you call VARARGPARSE.
%
% There are many, many ways to process optional arguments. The built-in
% INPUTPARSER works well but is quite verbose. See more discussion on
% http://stackoverflow.com/q/2775263/2514228.
%
% Example
% % Define function that takes varargs
% function [] = foo(varargin)
% params = {'baz','qux'};
% defaults = {1, 3};
% varargparse(varargin, params, defaults);
% disp(baz)
% disp(qux)
% end
%
% % Call function with param-value pairs.
% foo('bar', 2);
%
% See also INPUTPARSER, VALIDATESTRING, VALIDATEATTRIBUTES
% Created by Micah Smith
expected = params;
% Ensure we have a matching number of params and values.
if mod(length(args),2) ~= 0
error('Invalid param-value arguments.');
end
% Extract provided params (odd elements) and values (even elements)
params = args(1:2:end);
values = args(2:2:end);
% For each expected param, either pop it from the provided params or set it to
% its default value.
for i=1:length(expected);
key = expected{i};
if ismember(key, params)
j = find(strcmpi(key, params));
assignin('caller', key, values{j});
params(j) = [];
values(j) = [];
else
assignin('caller', key, defaults{i});
end
end
% Any additional param-value pairs are invalid.
if length(params) > 0
error(['Invalid param(s): ', params{:}]);
end
end % of varargparse