Home > bml > utils > bml_getopt.m

bml_getopt

PURPOSE ^

BML_GETOPT gets the value from a configuration structure [internal]

SYNOPSIS ^

function val = bml_getopt(opt, key, default, emptymeaningful)

DESCRIPTION ^

 BML_GETOPT gets the value from a configuration structure [internal]

 Use as
   val = bml_getopt(s, key, default, emptymeaningful)
 where the input values are
   s               = structure or cell-array
   key             = string
   default         = any valid MATLAB data type (optional, default = [])
   emptymeaningful = boolean value (optional, default = 0)

 If the key is present as field in the structure, or as key-value pair in the
 cell-array, the corresponding value will be returned.

 If the key is not present, bml_getopt will return the default, or an empty array
 when no default was specified.

 If the key is present but has an empty value, then the emptymeaningful flag
 specifies whether the empty value or the default value should be returned.
 If emptymeaningful==true, then the empty array will be returned.
 If emptymeaningful==false, then the specified default will be returned.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function val = bml_getopt(opt, key, default, emptymeaningful)
0002 
0003 % BML_GETOPT gets the value from a configuration structure [internal]
0004 %
0005 % Use as
0006 %   val = bml_getopt(s, key, default, emptymeaningful)
0007 % where the input values are
0008 %   s               = structure or cell-array
0009 %   key             = string
0010 %   default         = any valid MATLAB data type (optional, default = [])
0011 %   emptymeaningful = boolean value (optional, default = 0)
0012 %
0013 % If the key is present as field in the structure, or as key-value pair in the
0014 % cell-array, the corresponding value will be returned.
0015 %
0016 % If the key is not present, bml_getopt will return the default, or an empty array
0017 % when no default was specified.
0018 %
0019 % If the key is present but has an empty value, then the emptymeaningful flag
0020 % specifies whether the empty value or the default value should be returned.
0021 % If emptymeaningful==true, then the empty array will be returned.
0022 % If emptymeaningful==false, then the specified default will be returned.
0023 
0024 % 2017.10.27 AB: adapted from fieldtrip
0025 %
0026 % Copyright (C) 2011-2012, Robert Oostenveld
0027 %
0028 % This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
0029 % for the documentation and details.
0030 %
0031 %    FieldTrip is free software: you can redistribute it and/or modify
0032 %    it under the terms of the GNU General Public License as published by
0033 %    the Free Software Foundation, either version 3 of the License, or
0034 %    (at your option) any later version.
0035 %
0036 %    FieldTrip is distributed in the hope that it will be useful,
0037 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
0038 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0039 %    GNU General Public License for more details.
0040 %
0041 %    You should have received a copy of the GNU General Public License
0042 %    along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
0043 %
0044 % $Id$
0045 
0046 if nargin<3
0047   default = [];
0048 end
0049 
0050 if nargin < 4
0051   emptymeaningful = 0;
0052 end
0053 
0054 if isa(opt, 'struct') || isa(opt, 'config')
0055   % get the key-value from the structure
0056   fn = fieldnames(opt);
0057   if ~any(strcmp(key, fn))
0058     val = default;
0059   else
0060     val = opt.(key);
0061   end
0062   
0063 elseif istable(opt)
0064   assert(height(opt)==1,'configuration tables should be a 1 row high');
0065   if ~any(strcmp(key, opt.Properties.VariableNames))
0066     val = default;
0067   else
0068     val = opt.(key);
0069     if iscell(val)
0070       val = val{:};
0071     end
0072   end
0073   
0074 elseif isa(opt, 'cell')
0075   % get the key-value from the cell-array
0076   if mod(length(opt),2)
0077     error('optional input arguments should come in key-value pairs, i.e. there should be an even number');
0078   end
0079   
0080   % the 1st, 3rd, etc. contain the keys, the 2nd, 4th, etc. contain the values
0081   keys = opt(1:2:end);
0082   vals = opt(2:2:end);
0083   
0084   % the following may be faster than cellfun(@ischar, keys)
0085   valid = false(size(keys));
0086   for i=1:numel(keys)
0087     valid(i) = ischar(keys{i});
0088   end
0089   
0090   if ~all(valid)
0091     error('optional input arguments should come in key-value pairs, the optional input argument %d is invalid (should be a string)', i);
0092   end
0093   
0094   hit = find(strcmpi(key, keys));
0095   if isempty(hit)
0096     % the requested key was not found
0097     val = default;
0098   elseif length(hit)==1
0099     % the requested key was found
0100     val = vals{hit};
0101   else
0102     error('multiple input arguments with the same name');
0103   end
0104   
0105 elseif isempty(opt)
0106   % no options are specified, return default
0107   val = default;
0108 end % isstruct or iscell or isempty
0109 
0110 if isempty(val) && ~isempty(default) && ~emptymeaningful
0111   % use the default value instead of the empty input that was specified:
0112   % this applies for example if you do functionname('key', []), where
0113   % the empty is meant to indicate that the user does not know or care
0114   % what the value is
0115   val = default;
0116 end
0117 
0118 %returning cell of char instead of strings for compatibility with fieldtrip
0119 if isstring(val) || iscellstr(val) || ischar(val)
0120   val = cellstr(val);
0121 end
0122 
0123

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005