|
| 1 | +function [err] = argterr(func, arg, datatypes, arg_size, mode, msg, return_type) |
| 2 | +% argterr - Check the data type and size of a function argument. |
| 3 | +% |
| 4 | +%****f* wmtsa.utils/argterr |
| 5 | +% |
| 6 | +% NAME |
| 7 | +% argterr - Check the data type and size of a function argument. |
| 8 | +% |
| 9 | +% SYNOPSIS |
| 10 | +% [errmsg] = argterr(func, arg, type, [arg_size], [mode]) |
| 11 | +% [errmsg] = argterr(func, arg, type, [arg_size], [mode], [msg], 'string') |
| 12 | +% [errstruct] = argterr(func, arg, type, [arg_size], [mode], [msg], 'struct') |
| 13 | +% |
| 14 | +% INPUTS |
| 15 | +% * func -- checked function (string or function handle). |
| 16 | +% * arg -- the argument to check (object) |
| 17 | +% * datatypes -- expected data type(s) of the arg. |
| 18 | +% See verify_datatypes function for possibles datatypes to check. |
| 19 | +% (string or string cell array). |
| 20 | +% * arg_size -- (optional) expected size of array (integer vector). |
| 21 | +% * mode -- (optional) output display mode (charcter string) |
| 22 | +% * msg -- (optional) message string to be displayed (string). |
| 23 | +% * return_type -- (optional) type of output argument (string). |
| 24 | +% 'string' = return error message string (default). |
| 25 | +% 'struct' = return error message struct. |
| 26 | +% |
| 27 | +% OUTPUTS |
| 28 | +% * errmsg -- error message (string). |
| 29 | +% * errstruct -- error struct with fields: message, identifier. |
| 30 | +% |
| 31 | +% SIDE EFFECTS |
| 32 | +% Function call requires a minimum of three input arguments; otherwise error. |
| 33 | +% |
| 34 | +% DESCRIPTION |
| 35 | +% argterr checks the data type(s) and optionally the size of an argument to a |
| 36 | +% function call. If arg does not have the specified data type(s), an error |
| 37 | +% message or error struct is returned. |
| 38 | +% |
| 39 | +% arg may be a single |
| 40 | +% |
| 41 | +% Possible values for 'datatypes' to check include: |
| 42 | +% * 'posint' -- All are positive integers --> integer value(s) > 0. |
| 43 | +% * 'int0' -- All are positive integers plus zero --> integer value(s) >= 0. |
| 44 | +% * 'int','integer' -- All are integers --> any integer value(s). |
| 45 | +% * 'num','numeric' -- All are numeric. |
| 46 | +% * 'struct','structure' -- Is a structure. |
| 47 | +% * 'char','character','string' - Is a character string. |
| 48 | +% * 'scalar -- Is a point (size of all dimensions = 1). |
| 49 | +% * 'vec','vector' -- Is a vector (i.e. MxN, with M and/or N = 1). |
| 50 | +% * 'nonsingleton','truevector' -- Is a vector (i.e. MxN with M *or* N = 1). |
| 51 | +% * 'row','rowvector' -- Is a row vector (i.e. M x 1). |
| 52 | +% * 'col','columnvector' -- Is a column vector (i.e. 1 x N). |
| 53 | +% * 'finite' -- All are finite. |
| 54 | +% * 'nonsparse' -- Is a non-sparse matrix. |
| 55 | +% |
| 56 | +% The input argument 'mode' controls the display of diagnostic information |
| 57 | +% to the consolue and has possible values: |
| 58 | +% * 0 -- silent |
| 59 | +% * 1 -- verbose |
| 60 | +% The default value is 1 (silent). |
| 61 | +% |
| 62 | +% EXAMPLE |
| 63 | +% arg = [0 2]; |
| 64 | +% % arg is an integer. |
| 65 | +% err = argterr('myfunction', arg, 'posint') |
| 66 | +% % Result: err = 1 |
| 67 | +% err = argterr('myfunction', arg, 'int', [1 2], '') |
| 68 | +% % Result: err = 0 |
| 69 | +% [err, errmsg] = argterr('myfunction', arg, {'int', 'vector'}) |
| 70 | +% % Result: err = 0 |
| 71 | +% |
| 72 | +% NOTES |
| 73 | +% argterr is modelled after errargt, which is part of MATLAB wavelet toolkit. |
| 74 | +% |
| 75 | +% SEE ALSO |
| 76 | +% verify_datatype |
| 77 | +% |
| 78 | + |
| 79 | +% AUTHOR |
| 80 | +% Charlie Cornish |
| 81 | +% |
| 82 | +% CREATION DATE |
| 83 | +% 2003-06-22 |
| 84 | +% |
| 85 | +% COPYRIGHT |
| 86 | +% (c) 2003, 2004, 2005 Charles R. Cornish |
| 87 | +% |
| 88 | +% CREDITS |
| 89 | +% argterr is inspired by the function errargt |
| 90 | +% by M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi |
| 91 | +% which is part of the MATLAB wavelet toolkit. |
| 92 | +% |
| 93 | +% MATLAB VERSION |
| 94 | +% 7.0 |
| 95 | +% |
| 96 | +% REVISION |
| 97 | +% $Revision: 612 $ |
| 98 | +% |
| 99 | +%*** |
| 100 | + |
| 101 | +% $Id: argterr.m 612 2005-10-28 21:42:24Z ccornish $ |
| 102 | + |
| 103 | +%% Set Defaults |
| 104 | +err = []; |
| 105 | +valid_mode = [0 1]; |
| 106 | +valid_return_type = {'string', 'struct'}; |
| 107 | + |
| 108 | +usage_str = ['Usage: [err, errmsg] = ', mfilename, ... |
| 109 | + '(func, arg, datatypes, [arg_size], [mode], [msg], [return_type])']; |
| 110 | + |
| 111 | +%% Check arguments |
| 112 | +error(nargerr(mfilename, nargin, [3:7], nargout, [0:1], ... |
| 113 | + 1, usage_str, 'struct')); |
| 114 | + |
| 115 | +if (ischar(func)) |
| 116 | + funcname = func; |
| 117 | +elseif (isa(func, 'function_handle')) |
| 118 | + funcname = func2str(func); |
| 119 | +else |
| 120 | + error('WMTSA:nargerr:invalidInputArgumentValue', ... |
| 121 | + 'func must be a string or function handle'); |
| 122 | +end |
| 123 | + |
| 124 | +arg_name = inputname(2); |
| 125 | + |
| 126 | +if (iscellstr(datatypes)) |
| 127 | + % OK - do nothing |
| 128 | +elseif (ischar(datatypes)) |
| 129 | + % Convert to character cell array. |
| 130 | + datatypes = {datatypes}; |
| 131 | +else |
| 132 | + error('WMTSA:invalidArgumentType', ... |
| 133 | + ['Argument ''datatypes'' must be a charcter string or charcter cell ' ... |
| 134 | + 'array.']); |
| 135 | +end |
| 136 | + |
| 137 | +if (~exist('mode', 'var')) |
| 138 | + mode = 1; |
| 139 | +end |
| 140 | + |
| 141 | +if (isempty(find(mode == valid_mode))) |
| 142 | + err_s.message = ['Invalid value for input argument ', 'mode']; |
| 143 | + err_s.identifier = ['WMTSA:nargerr:invalidInputArgumentValue']; |
| 144 | + error(err_s); |
| 145 | +end |
| 146 | + |
| 147 | +if (~exist('return_type', 'var') || isempty(return_type)) |
| 148 | + return_type = 'string'; |
| 149 | +end |
| 150 | + |
| 151 | +if (isempty(strmatch(return_type, valid_return_type))) |
| 152 | + err_s.message = ['Invalid value for input argument ', 'return_type']; |
| 153 | + err_s.identifier = ['WMTSA:nargerr:invalidInputArgumentValue']; |
| 154 | + error(err_s); |
| 155 | +end |
| 156 | + |
| 157 | +% Check argument datatypes |
| 158 | +[tf, errmsg] = verify_datatype(arg, datatypes, arg_name); |
| 159 | + |
| 160 | +err_s = []; |
| 161 | + |
| 162 | +if (~tf) |
| 163 | + err_s.identifier = ['WMTSA:argterr:invalidArgumentDataType']; |
| 164 | + err_s.message = errmsg; |
| 165 | +else |
| 166 | + % Check argument dimension sizes |
| 167 | + if (exist('arg_size', 'var') && ~isempty(arg_size)) |
| 168 | + actual_arg_size = size(arg); |
| 169 | + if (~isequal(actual_arg_size, arg_size)) |
| 170 | + err_s.identifier = ['WMTSA:argterr:incorrectArgumentDimensions']; |
| 171 | + err_s.message = ['Expected and actual sizes of ', arg_name, ' are not equal']; |
| 172 | + end |
| 173 | + end |
| 174 | +end |
| 175 | + |
| 176 | +if (~isempty(err_s)) |
| 177 | + |
| 178 | + if (mode) |
| 179 | + display_errmsg(funcname, err_s.message) |
| 180 | + end |
| 181 | + |
| 182 | + if(exist('msg', 'var')) |
| 183 | + disp(msg); |
| 184 | + end |
| 185 | + |
| 186 | + if (strmatch(return_type, 'string')) |
| 187 | + err = err_s.message; |
| 188 | + elseif (strmatch(return_type, 'struct')) |
| 189 | + err = err_s; |
| 190 | + end |
| 191 | +end |
| 192 | + |
| 193 | + |
| 194 | +return |
| 195 | + |
| 196 | + |
| 197 | +function display_errmsg(funcname, errmsg) |
| 198 | + |
| 199 | + blank = ' '; |
| 200 | + |
| 201 | + if (~isempty(deblank(funcname))) |
| 202 | + msg = [' ', funcname, ': ', xlate(errmsg)]; |
| 203 | + else |
| 204 | + msg = errmsg; |
| 205 | + end |
| 206 | + |
| 207 | + msg_length = length(msg); |
| 208 | + banner_length = msg_length + 2; |
| 209 | + star_str = '*'; |
| 210 | + star_str = star_str(:, ones(1, banner_length)); |
| 211 | + minus_str = '-'; |
| 212 | + minus_str = minus_str(:, ones(1, banner_length)); |
| 213 | + |
| 214 | + |
| 215 | + disp(blank); |
| 216 | + disp(star_str); |
| 217 | + disp('ARGUMENT TYPE ERROR ...'); |
| 218 | + disp(minus_str); |
| 219 | + disp(msg); |
| 220 | + disp(star_str); |
| 221 | + disp(blank); |
| 222 | + |
| 223 | +return |
| 224 | + |
| 225 | + |
| 226 | + |
0 commit comments