time | Calls | line |
---|
| | 1 | function [varargout]=parseArgs(pnames,dflts,varargin)
|
| | 2 | %PARSEARGS Process parameter name/value pairs for table methods/functions
|
| | 3 | % [A,B,...] = parseArgs(PNAMES,DFLTS,'NAME1',VAL1,'NAME2',VAL2,...)
|
| | 4 | % In typical use there are N output values, where PNAMES is a cell array
|
| | 5 | % of N valid parameter names, and DFLTS is a cell array of N default
|
| | 6 | % values for these parameters. The remaining arguments are parameter
|
| | 7 | % name/value pairs that were passed into the caller. The N outputs
|
| | 8 | % [A,B,...] are assigned in the same order as the names in PNAMES.
|
| | 9 | % Outputs corresponding to entries in PNAMES that are not specified
|
| | 10 | % in the name/value pairs are set to the corresponding value from DFLTS.
|
| | 11 | % Unrecognized name/value pairs are an error.
|
| | 12 | %
|
| | 13 | % [A,B,...,SETFLAG] = parseArgs(...), where SETFLAG is the N+1 output
|
| | 14 | % argument, also returns a structure with a field for each parameter
|
| | 15 | % name. The value of the field indicates whether that parameter was
|
| | 16 | % specified in the name/value pairs (true) or taken from the defaults
|
| | 17 | % (false).
|
| | 18 | %
|
| | 19 | % [A,B,...,SETFLAG,EXTRA] = parseArgs(...), where EXTRA is the N+2 output
|
| | 20 | % argument, accepts parameter names that are not listed in PNAMES. These
|
| | 21 | % are returned in the output EXTRA as a cell array.
|
| | 22 | %
|
| | 23 | % Example:
|
| | 24 | % pnames = {'color' 'linestyle', 'linewidth'}
|
| | 25 | % dflts = { 'r' '_' '1'}
|
| | 26 | % varargin = {'linew' 2 'linestyle' ':'}
|
| | 27 | % [c,ls,lw] = matlab.internal.datatypes.parseArgs(pnames,dflts,varargin{:})
|
| | 28 | % % On return, c='r', ls=':', lw=2
|
| | 29 | %
|
| | 30 | % [c,ls,lw,sf] = matlab.internal.datatypes.parseArgs(pnames,dflts,varargin{:})
|
| | 31 | % % On return, sf = [false true true]
|
| | 32 | %
|
| | 33 | % varargin = {'linew' 2 'linestyle' ':' 'special' 99}
|
| | 34 | % [c,ls,lw,sf,ex] = matlab.internal.datatypes.parseArgs(pnames,dflts,varargin{:})
|
| | 35 | % % On return, ex = {'special' 99}
|
| | 36 |
|
| | 37 | % Copyright 2012-2018 The MathWorks, Inc.
|
| | 38 |
|
| | 39 |
|
| | 40 | % Initialize some variables
|
< 0.001 | 8 | 41 | nparams = length(pnames);
|
< 0.001 | 8 | 42 | varargout = dflts;
|
< 0.001 | 8 | 43 | setflag = false(1,nparams);
|
< 0.001 | 8 | 44 | unrecog = {};
|
< 0.001 | 8 | 45 | nargs = length(varargin);
|
| | 46 |
|
< 0.001 | 8 | 47 | SuppliedRequested = nargout > nparams;
|
< 0.001 | 8 | 48 | UnrecognizedRequested = nargout > (nparams+1);
|
| | 49 |
|
| | 50 | % Must have name/value pairs
|
< 0.001 | 8 | 51 | if mod(nargs,2)~=0
|
| | 52 | m = message('MATLAB:table:parseArgs:WrongNumberArgs');
|
| | 53 | throwAsCaller(MException(m.Identifier, '%s', getString(m)));
|
< 0.001 | 8 | 54 | end
|
< 0.001 | 8 | 55 | ischars = false(1,nargs);
|
< 0.001 | 8 | 56 | for i = 1:2:nargs
|
| | 57 | % {'foo'} is not allowed. We just want to find only scalar text - char
|
| | 58 | % vectors and scalar strings.
|
< 0.001 | 8 | 59 | ischars(i) = ischar(varargin{i});
|
< 0.001 | 8 | 60 | end
|
0.001 | 8 | 61 | [varargin{ischars}] = convertCharsToStrings(varargin{ischars});
|
| | 62 | % Process name/value pairs
|
< 0.001 | 8 | 63 | for j=1:2:nargs
|
< 0.001 | 8 | 64 | pname = varargin{j};
|
0.001 | 8 | 65 | if ~(isstring(pname) && isscalar(pname) && strlength(pname) > 0)
|
| | 66 | throwAsCaller(MException(message('MATLAB:table:parseArgs:IllegalParamName')));
|
< 0.001 | 8 | 67 | end
|
| | 68 |
|
< 0.001 | 8 | 69 | mask = startsWith(pnames,pname,'IgnoreCase',true); % look for partial match
|
< 0.001 | 8 | 70 | if ~any(mask)
|
| | 71 | if UnrecognizedRequested
|
| | 72 | % If they've asked to get back unrecognized names/values, add this
|
| | 73 | % one to the list.
|
| | 74 | unrecog((end+1):(end+2)) = {char(varargin{j}) varargin{j+1}};
|
| | 75 | continue
|
| | 76 | else % otherwise, it's an error
|
| | 77 | throwAsCaller(MException(message('MATLAB:table:parseArgs:BadParamName',pname)));
|
| | 78 | end
|
< 0.001 | 8 | 79 | elseif sum(mask) > 1
|
| | 80 | mask = strcmpi(pname,pnames); % use exact match to resolve ambiguity
|
| | 81 | if sum(mask) ~= 1
|
| | 82 | throwAsCaller(MException(message('MATLAB:table:parseArgs:AmbiguousParamName',pname)));
|
| | 83 | end
|
< 0.001 | 8 | 84 | end
|
< 0.001 | 8 | 85 | varargout{mask} = varargin{j+1};
|
< 0.001 | 8 | 86 | setflag(mask) = true;
|
< 0.001 | 8 | 87 | end
|
| | 88 |
|
| | 89 | % Return extra stuff if requested
|
< 0.001 | 8 | 90 | if SuppliedRequested
|
< 0.001 | 8 | 91 | for kk = 1:numel(pnames)
|
0.001 | 16 | 92 | supplied.(pnames{kk}) = setflag(kk);
|
< 0.001 | 16 | 93 | end
|
< 0.001 | 8 | 94 | varargout{nparams+1} = supplied;
|
< 0.001 | 8 | 95 | if UnrecognizedRequested
|
| | 96 | varargout{nparams+2} = unrecog;
|
< 0.001 | 8 | 97 | end
|
< 0.001 | 8 | 98 | end
|
Other subfunctions in this file are not included in this listing.