-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_to_parse_param_value_pairs.m
39 lines (37 loc) · 1.3 KB
/
template_to_parse_param_value_pairs.m
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
function [param1,param2,param3] = template_to_parse_param_value_pairs(varargin)
% Input:
% varargin Parameter-Value pairs as detailed below.
%
% Parameter-Value pairs:
% Parameter: 'Param1'
% Value: 'Default' - (Default) Chooses default option.
% 'Option2' - Chooses the 2nd option.
% Parameter: 'Param2'
% Value: Any number. Default: 1.
% Parameter: 'Param3'
% Value: Some cell array. Default: {1, 'default'}.
% Handle parameter-value pairs
param1 = lower('Default');
param2 = 1;
param3 = {1, 'default'};
assert(mod(length(varargin),2) == 0, 'Even number of varargin arguments expected.')
for i = 1:2:length(varargin)
param = lower(varargin{i});
value = varargin{i+1};
if ischar(value) || (iscell(value) && all(cellfun(@(a) ischar(a), value)))
value = lower(value);
end
switch param
case 'param1'
assert(any(strcmpi(value,{'Default','Option2'})), ...
['Unexpected value for ' param ' parameter.'])
param1 = value;
case 'param2'
param2 = value;
case 'param3'
param3 = value;
otherwise
error(['Invalid parameter: ' param '.'])
end
end
end