-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_opt.m
executable file
·166 lines (147 loc) · 4.24 KB
/
check_opt.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function [ok, arg] = check_opt( who, what, varargin )
%check_opt - verify option type and arrange correct output
%
% 'what' is the description of values classes 'who' can take
% f.i.: what = { { 'char' } , { 'cell' } ]
%
% varargin{1} = return type
%
% BITS - Banca d'Italia Time Series
% Copyright 2005-2012 Banca d'Italia - Area Ricerca Economica e Relazioni Internazionali
%
% Author: Emmanuele Somma (emmanuele_DOT_somma_AT_bancaditalia_DOT_it)
% Area Ricerca Economica e Relazioni Internazionali
% Banca d'Italia
%
if nargin>2
rettype = varargin{1};
else
rettype = 'same';
end
tfunc = nan;
ttype = nan;
transform = nan;
% Function to appy to results (if any)
%
if nargin>3
tfunc = varargin{2};
ttype = tfunc{1};
transform = tfunc{2};
tfunc = true;
end
if isa(what,'char')
what = { what };
end
ok = false;
arg = nan;
choice = nan ;
type = nan;
% Test on type
%
for i=1:length(what)
choice = what{i};
type = choice{1};
if isa(who,type)
ok=true;
break;
end
end
if ~ok % exit with type-error
arg = 'type';
return
end
% Test on Size
%
for i=1:length(what)
choice = what{i};
if ok & length(choice)>1
len = choice{2}
if length(len)>1
ok = isequal( size(who) , len );
else
ok = isequal( length(who) , len );
end
end
end
if ~ok % exit with size-error
arg = 'size';
return
end
% Transform return
%
if ok
arg = who;
if strcmp(rettype,'char')
if ~isa(who,'char')
arg = char(who);
end
% Apply func to result
if ~isnan(tfunc)
arg = sprintf('arg = %s(''%s'')',transform,arg);
eval( [ arg ]);
end
elseif strcmp(rettype,'cell')
if ~isa(who,'cell')
arg = { who };
end
% Apply func to result
if ~isnan(tfunc)
% FUNCTION datenum
if strcmp(transform,'datenum')
for i=1:length(arg)
v = arg{i};
if isscalar(v)
if ~isa(v,ttype)
cmd = sprintf('arg{%d} = %s(''%s'');',i,transform,v);
eval( [ cmd ] );
end
else
w = [];
if isa(v,'char')
cmd = sprintf('arg{%d} = %s(''%s'');',i,transform,v);
eval( [ cmd ] );
else
for m=1:size(v,1)
for n=1:size(v,2)
if ~isa(v(m,n),ttype)
cmd = sprintf('w(%d,%d) = %s(''%s'');',m,n,transform,v(m,n))
eval( [ cmd ] );
end
end
end
end
end
end
% FUNCTION date2tsidx
elseif strcmp(transform,'date2tsidx')
freq = varargin{3};
if isnan(freq)
error([ mfilename '::Internal error in check_args']);
end
for i=1:length(arg)
v = arg{i};
if isnan(v)
arg{i}=nan;
continue
end
if isscalar(v)
if ~isa(v,ttype)
cmd = sprintf('arg{%d} = %s(''%s'');',i,transform,v);
eval( [ cmd ] );
end
else
if ~isa(v,ttype)
cmd = sprintf('[du dy] = %s(%d,%d);',transform,freq,datenum(v));
eval( [ cmd ] );
arg{i}=[du dy];
else
arg{i} = v;
end
end
end
end
end
else
%
end
end