-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseVarargin.m
63 lines (54 loc) · 1.86 KB
/
parseVarargin.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function arg=parseVarargin(Var,arg,casesen)
% function arg=parseVarargin(var,arg)
% parses any additional input argument that came in property/value pair
% arg is a struct where field names ar eproperty names and values ar
% edefault values.
%
% EXAMPLE:
% arg=parseVarargin(varargin,arg);
% if varargin was empty - this will return whatever defaults where given by
% arg.
if isempty(Var)
return
end
% if varargin first item is a struct - assumes it should repace
% arg as new defaults and return. useful for recursive calls of functions
% just pass the arg
if numel(Var)==1 && isstruct(Var{1})
arg=Var{1};
end
if nargin<3
casesen=0;
end
% get rid of any possible other optional arguemnt that are not in
% property/value pair. parseparams remove all the cells up to (not
% including) the first string one assumeing it is the first property name
[~,Var]=parseparams(Var);
% test if all fields in var are fields in arg
if ~casesen
unknown=setdiff(lower(Var(1:2:end)),lower(fieldnames(arg)));
else
unknown=setdiff(Var(1:2:end),fieldnames(arg));
end
if ~isempty(unknown)
str=sprintf('Unknown fields in input arguments:\n');
for i=1:length(unknown)
str=sprintf('%s %s\n',str,unknown{i});
end
error(str);
end
% update any fields
for i=1:2:length(Var)
% check that new argument is of same class is
if ~casesen
if ~isequal(class(arg.(lower(Var{i}))),class(Var{i+1}))
warning('parsevarargin:class','Argument %s require value of class %s',Var{i},class(arg.(Var{i}))); %#ok<*WNTAG>
end
arg.(lower(Var{i}))=Var{i+1};
else
if ~isequal(class(arg.(Var{i})),class(Var{i+1}))
warning('parsevarargin:class','Argument %s require value of class %s',Var{i},class(arg.(Var{i})));
end
arg.(Var{i})=Var{i+1};
end
end