eharley0142

optimget.m

May 10th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.25 KB | None | 0 0
  1. % Copyright (C) 2008 Jaroslav Hajek
  2. % Copyright (C) 2009 VZLU Prague
  3. %
  4. % This file is part of Octave.
  5. %
  6. % Octave is free software; you can redistribute it and/or modify it
  7. % under the terms of the GNU General Public License as published by
  8. % the Free Software Foundation; either version 3 of the License, or (at
  9. % your option) any later version.
  10. %
  11. % Octave is distributed in the hope that it will be useful, but
  12. % WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. % General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU General Public License
  17. % along with Octave; see the file COPYING.  If not, see
  18. % <http://www.gnu.org/licenses/>.
  19.  
  20. % -*- texinfo -*-
  21. % @deftypefn {Function File} {} optimget (@var{options}, @var{parname})
  22. % @deftypefnx {Function File} {} optimget (@var{options}, @var{parname}, @var{default})
  23. % Return a specific option from a structure created by
  24. % @code{optimset}.  If @var{parname} is not a field of the @var{options}
  25. % structure, return @var{default} if supplied, otherwise return an
  26. % empty matrix.
  27. % @end deftypefn
  28.  
  29. function retval = optimget (options, parname, default)
  30.  
  31.   if (nargin < 2 || nargin > 4 || ~ isstruct (options) || ~ ischar (parname))
  32.     print_usage ();
  33.   end
  34.  
  35.   % opts = all_opts__ ();
  36.   opts = { 'ComplexEqn', 'FinDiffType', 'FunValCheck', 'GradObj', 'Jacobian', 'MaxFunEvals', 'MaxIter', 'OutputFcn', 'TolFun', 'TolX', 'Updating' };
  37.   idx = lookup (opts, parname, 'i');
  38.  
  39.   if (idx > 0 && strcmpi (parname, opts{idx}))
  40.     parname = opts{idx};
  41.   else
  42.     warning ('unrecognized option: %s', parname);
  43.   end
  44.   if (isfield (options, parname))
  45.     retval = options.(parname);
  46.   elseif (nargin > 2)
  47.     retval = default;
  48.   else
  49.     retval = [];
  50.   end
  51.  
  52. end
  53.  
  54. function idx = lookup( opts, parname, caseq )
  55.     if ( strcmp('caseq','i')  )
  56.         for i = 1:length(opts)
  57.             if ( strcmpi(opts{i},parname)  )
  58.                 idx = i;
  59.                 return;
  60.             end
  61.         end
  62.     else
  63.         for i = 1:length(opts)
  64.             if ( strcmp(opts{i},parname)  )
  65.                 idx = i;
  66.                 return;
  67.             end
  68.         end
  69.     end
  70.     idx = -1;
  71.     return;
  72. end
Advertisement
Add Comment
Please, Sign In to add comment