time | calls | line |
---|
| | 1 | function [f,msg] = fcnchk(fun,varargin)
|
| | 2 | %FCNCHK Check FUNFUN function argument.
|
| | 3 | %
|
| | 4 | % FCNCHK will not accept string expressions for FUN in a future
|
| | 5 | % release. Use anonymous functions for FUN instead.
|
| | 6 | %
|
| | 7 | % FCNCHK(FUN,...) returns an inline object based on FUN if FUN
|
| | 8 | % is a string containing parentheses, variables, and math
|
| | 9 | % operators. FCNCHK simply returns FUN if FUN is a function handle,
|
| | 10 | % or a MATLAB object with an feval method (such as an inline object).
|
| | 11 | % If FUN is a string name of a function (e.g. 'sin'), FCNCHK returns a
|
| | 12 | % function handle to that function.
|
| | 13 | %
|
| | 14 | % FCNCHK is a helper function for FMINBND, FMINSEARCH, FZERO, etc. so they
|
| | 15 | % can compute with string expressions in addition to functions.
|
| | 16 | %
|
| | 17 | % FCNCHK(FUN,...,'vectorized') processes the string (e.g., replacing
|
| | 18 | % '*' with '.*') to produce a vectorized function.
|
| | 19 | %
|
| | 20 | % When FUN contains an expression then FCNCHK(FUN,...) is the same as
|
| | 21 | % INLINE(FUN,...) except that the optional trailing argument 'vectorized'
|
| | 22 | % can be used to produce a vectorized function.
|
| | 23 | %
|
| | 24 | % [F,ERR] = FCNCHK(...) returns a struct array ERR. This struct is empty
|
| | 25 | % if F was constructed successfully. ERR can be used with ERROR to throw
|
| | 26 | % an appropriate error message if F was not constructed successfully.
|
| | 27 | %
|
| | 28 | % See also ERROR, INLINE, @, FUNCTION_HANDLE.
|
| | 29 |
|
| | 30 | % Copyright 1984-2012 The MathWorks, Inc.
|
| | 31 |
|
| 26 | 32 | msgident = '';
|
| | 33 |
|
| 26 | 34 | nin = nargin;
|
| 26 | 35 | if nin > 1 && strcmp(varargin{end},'vectorized')
|
| | 36 | vectorizing = true;
|
| | 37 | nin = nin - 1;
|
| 26 | 38 | else
|
| 26 | 39 | vectorizing = false;
|
| 26 | 40 | end
|
| | 41 |
|
| 26 | 42 | if ischar(fun)
|
| | 43 | fun = strtrim_local_function(fun);
|
| | 44 | % Check for non-alphanumeric characters that must be part of an
|
| | 45 | % expression.
|
| | 46 | if isempty(fun),
|
| | 47 | f = inline('[]');
|
| | 48 | elseif ~vectorizing && isidentifier_local_function(fun)
|
| | 49 | f = str2func(fun); % Must be a function name only
|
| | 50 | % Note that we avoid collision of f = str2func(fun) with any local
|
| | 51 | % function named fun, by uglifying the local function's name
|
| | 52 | if isequal('x',fun)
|
| | 53 | warning(message('MATLAB:fcnchk:AmbiguousX'));
|
| | 54 | end
|
| | 55 | else
|
| | 56 | if vectorizing
|
| | 57 | f = inline(vectorize(fun),varargin{1:nin-1});
|
| | 58 | var = argnames(f);
|
| | 59 | f = inline([formula(f) '.*ones(size(' var{1} '))'],var{1:end});
|
| | 60 | else
|
| | 61 | f = inline(fun,varargin{1:nin-1});
|
| | 62 | end
|
| | 63 | end
|
| 26 | 64 | elseif isa(fun,'function_handle')
|
| 26 | 65 | f = fun;
|
| | 66 | % is it a MATLAB object with a feval method?
|
| | 67 | elseif isobject(fun)
|
| | 68 | % delay the methods call unless we know it is an object to avoid
|
| | 69 | % runtime error for compiler
|
| | 70 | [meths,cellInfo] = methods(class(fun),'-full');
|
| | 71 | if ~isempty(cellInfo) % if fun is a MATLAB object
|
| | 72 | meths = cellInfo(:,3); % get methods names from cell array
|
| | 73 | end
|
| | 74 | if any(strmatch('feval',meths))
|
| | 75 | if vectorizing && any(strmatch('vectorize',meths))
|
| | 76 | f = vectorize(fun);
|
| | 77 | else
|
| | 78 | f = fun;
|
| | 79 | end
|
| | 80 | else % no feval method
|
| | 81 | f = '';
|
| | 82 | msgident = 'MATLAB:fcnchk:objectMissingFevalMethod';
|
| | 83 | end
|
| | 84 | else
|
| | 85 | f = '';
|
| | 86 | msgident = 'MATLAB:fcnchk:invalidFunctionSpecifier';
|
| | 87 | end
|
| | 88 |
|
| | 89 | % If no errors and nothing to report then we are done.
|
| 26 | 90 | if nargout < 2 && isempty(msgident)
|
| 26 | 91 | return
|
| | 92 | end
|
| | 93 |
|
| | 94 | % compute MSG
|
| | 95 | if isempty(msgident)
|
| | 96 | msg.message = '';
|
| | 97 | msg.identifier = '';
|
| | 98 | msg = msg(zeros(0,1)); % make sure msg is the right dimension
|
| | 99 | else
|
| | 100 | msg.identifier = msgident;
|
| | 101 | msg.message = getString(message(msg.identifier));
|
| | 102 | end
|
| | 103 |
|
| | 104 | if nargout < 2
|
| | 105 | if ~isempty(msg)
|
| | 106 | error(message(msg.identifier));
|
| | 107 | end
|
| | 108 | end
|
Other subfunctions in this file are not included in this listing.