Home > atmlab > handy > optargs.m

optargs

PURPOSE ^

OPTARGS A help function to handle optional arguments

SYNOPSIS ^

function varargout = optargs( userin, defaults )

DESCRIPTION ^

 OPTARGS   A help function to handle optional arguments

    The function provides a simple way to define default values for function
    input. Only the cases of the type somefun(a,b,c,d) are handled. Use e.g.
    Matlab's inputParser for functions using parameter-value pairs (such as
    ('LineWidth',2) for plot).
    
    The length of *defaults* determines the number of optional arguments. The
    output is a merge of *userin* and *defaults*. Both these variables are cell
    vectors. Data are taken from *defaults* where *userin* has [] or is lacking
    a value. For example,
       [a,b] = optargs( {1,[]}, {0,'a'} )
    gives a=1 and b='a'. However, the normal usage of the function should
    be to set *userin* to *varargin*:
       [a,b] = optargs( varargin, {0,'a'} )

 FORMAT   varargout = optargs( userin, defaults )
        
 OUT   varargout  Merged input arguments.  
 IN    userin     User provided optional settings (captured by *varargin*).
       defaults   Cell array with default values for optional arguments.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

optargs.m

SOURCE CODE ^

0001 % OPTARGS   A help function to handle optional arguments
0002 %
0003 %    The function provides a simple way to define default values for function
0004 %    input. Only the cases of the type somefun(a,b,c,d) are handled. Use e.g.
0005 %    Matlab's inputParser for functions using parameter-value pairs (such as
0006 %    ('LineWidth',2) for plot).
0007 %
0008 %    The length of *defaults* determines the number of optional arguments. The
0009 %    output is a merge of *userin* and *defaults*. Both these variables are cell
0010 %    vectors. Data are taken from *defaults* where *userin* has [] or is lacking
0011 %    a value. For example,
0012 %       [a,b] = optargs( {1,[]}, {0,'a'} )
0013 %    gives a=1 and b='a'. However, the normal usage of the function should
0014 %    be to set *userin* to *varargin*:
0015 %       [a,b] = optargs( varargin, {0,'a'} )
0016 %
0017 % FORMAT   varargout = optargs( userin, defaults )
0018 %
0019 % OUT   varargout  Merged input arguments.
0020 % IN    userin     User provided optional settings (captured by *varargin*).
0021 %       defaults   Cell array with default values for optional arguments.
0022 
0023 % 2010-01-03   Created by Patrick Eriksson.
0024 
0025 
0026 function varargout = optargs( userin, defaults )
0027 
0028   
0029 %- Lengths
0030 %
0031 nin = length( userin );
0032 nd  = length( defaults );
0033                                                                             %&%
0034                                                                             %&%
0035 %- Checks                                                                   %&%
0036 %                                                                           %&%
0037 assert( iscell( userin ) );                                                 %&%
0038 assert( iscell( defaults ) );                                               %&%
0039 assert( nargout == nd );                                                    %&%
0040 %
0041 if nin > nd
0042   [st,i] = dbstack; 
0043   error( 'Too many input arguments for *%s*.', st(i+1).name );
0044 end 
0045 
0046 
0047 %- Set everything to default
0048 %
0049 varargout = defaults;
0050 
0051 
0052 %- Include user settings
0053 %
0054 for i =1:nin
0055   if ~isempty( userin{i} )
0056     varargout{i} = userin{i};
0057   end
0058 end

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