Repository navigation
Expand file tree
/
Copy pathpcm_getUserOptions.m
More file actions
73 lines (71 loc) · 2.4 KB
/
Copy pathpcm_getUserOptions.m
File metadata and controls
73 lines (71 loc) · 2.4 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
66
67
68
69
70
71
72
73
function options=pcm_getUserOptions(newOptions,defOptions,allowed);
% function Opt=getUserOptions(options,Opt);
%
% Deals with user-option structure. It allows the user to specify user
% options as a structure, or as a set of fieldnames and values
%
% INPUTS
% options: either a structure of user options or a cell array
% of varargins with 'fieldname1',value1,'fieldname2',value2 ...
% OPTIONAL:
% Opt: structure of default options
% allowed: Cell array of allowed fieldnames for error checking
%
% EXAMPLE:
% function myFunc(input,varargin);
% % Specify default options
% Opt.wdir = 'test';
% Opt.method = 1;
% % Allow user to overwrite defaults
% Opt = rsa_getUserOptions(varargin,Opt,{'wdir','method');
%
% % User can call this with
% myFunc(input,myOpts) Or
% myFunc(input,'wdir','root','method',2);
% myFunc(input,myOpts,'fieldname',value,otherOptions);
% Joern Diedrichsen
% j.diedrichsen@ucl.ac.uk
% 2/2015
if (nargin<3)
allowed=[];
end;
if (nargin<2)
defOptions = [];
end;
options = defOptions;
% Deal with option structure
c=1;
while c<=length(newOptions)
if (isstruct(newOptions{c}))
names = fieldnames(newOptions{c});
for f=1:length(names);
if ~isempty(allowed)
if ~any(strcmp(names{f},allowed));
msg = sprintf('User argument ''%s'' is not allowed',names{f})
msg = [msg sprintf('Allowed names are:\n')];
msg = [msg sprintf('%s\n',allowed{:})];
error(msg);
end;
end;
options.(names{f})=newOptions{c}.(names{f});
end;
c=c+1;
% Alternatively, deal with number of input strings
elseif (ischar(newOptions{c}))
if ~isempty(allowed)
if ~any(strcmp(newOptions{c},allowed));
msg = sprintf('User argument ''%s'' is not allowed',newOptions{c})
msg = [msg sprintf('Allowed names are:\n')];
msg = [msg sprintf('%s\n',allowed{:})];
error(msg);
end;
end;
if length(newOptions)==c
error(sprintf('Option ''%s'' must be followed by a value',newOptions{c}));
end;
options.(newOptions{c})=newOptions{c+1};
c=c+2;
else
error('Options must be either fieldnames or structures');
end;
end;