Home > atmlab > handy > optargs_struct.m

optargs_struct

PURPOSE ^

optargs_struct A help function to handle optional arguments in a struct

SYNOPSIS ^

function out = optargs_struct(userin, defaults, varargin)

DESCRIPTION ^

 optargs_struct  A help function to handle optional arguments in a struct

 This function provides a simply way to define default values for function
 inputs where this input consists of a structure. It takes two structures,
 one consisting of default values and one of user-provided values. It
 returns a structure containing the user values where provided and the
 default values otherwise.

 If for a given field, both the default and the user-input are a
 structure, optargs_struct recursively descends into substructures, where
 pairs of structures are passed on to optargs_struct.

 FORMAT

   out = optargs_struct(userin, defaults[, recurse])

 IN

   userin      structure   User provided optional settings
   defaults    structure   Default values.

 OPTIONAL INPUT

   recurse     bool        Recurse into substructures; defaults to true

 OUT

   merged      structure   User's value where provided, default otherwise

 See also: <a href="matlab:help optargs">optargs</a>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

optargs_struct.m

SOURCE CODE ^

0001 function out = optargs_struct(userin, defaults, varargin)
0002 
0003 % optargs_struct  A help function to handle optional arguments in a struct
0004 %
0005 % This function provides a simply way to define default values for function
0006 % inputs where this input consists of a structure. It takes two structures,
0007 % one consisting of default values and one of user-provided values. It
0008 % returns a structure containing the user values where provided and the
0009 % default values otherwise.
0010 %
0011 % If for a given field, both the default and the user-input are a
0012 % structure, optargs_struct recursively descends into substructures, where
0013 % pairs of structures are passed on to optargs_struct.
0014 %
0015 % FORMAT
0016 %
0017 %   out = optargs_struct(userin, defaults[, recurse])
0018 %
0019 % IN
0020 %
0021 %   userin      structure   User provided optional settings
0022 %   defaults    structure   Default values.
0023 %
0024 % OPTIONAL INPUT
0025 %
0026 %   recurse     bool        Recurse into substructures; defaults to true
0027 %
0028 % OUT
0029 %
0030 %   merged      structure   User's value where provided, default otherwise
0031 %
0032 % See also: <a href="matlab:help optargs">optargs</a>
0033 
0034 % Author: Gerrit Holl
0035 % $Id: optargs_struct.m 8934 2014-09-14 13:20:02Z patrick $
0036 
0037 recurse = optargs(varargin, {true});
0038 
0039 % If userin empty, out is simply set to default
0040 if isempty(userin)
0041   out = defaults;
0042   return;
0043 end
0044   
0045 out = defaults;
0046 fields_userin = fieldnames(userin);
0047 for i = 1:length(fields_userin)
0048     f = fields_userin{i};
0049     % special case: input and default are struct, then recurse
0050     % as far as I'm aware, structures cannot contain themselves, so this
0051     % cannot lead to any infinite loop (no need for explicit recurse
0052     % counting)
0053     if recurse && isstruct(userin.(f)) && isfield(defaults, f)
0054         if isstruct(defaults.(f))
0055             out.(f) = optargs_struct(userin.(f), defaults.(f));
0056             continue
0057         end
0058     end
0059     out.(f) = userin.(f);
0060 end
0061 
0062 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005