Home > atmlab > handy > rqre_datatype.m

rqre_datatype

PURPOSE ^

RQRE_DATATYPE Checks if variable is of any expected data types

SYNOPSIS ^

function rqre_datatype( a, funchandles, nonstdname )

DESCRIPTION ^

 RQRE_DATATYPE   Checks if variable is of any expected data types

    The input argument *funchandles* is a single or a cell array of handles to
    functions performing type checking, such as @isnumeric. For example, the
    following will generate an error message:
       a = 'a';
       rqre_datatype( a, {@isnumeric,@iscell} )

    Issues an error if criterion not fulfilled. This error assumes that all
    functions are named as isxxxx, where xxxx is the name of the data type.

    Use *<a href="matlab:help rqre_alltypes">rqre_alltypes</a>* to check if a combination of type checks is fullfilled.

 FORMAT   rqre_datatype( a, funchandles [,nonstdname] )
        
 IN    a             Variable to check.
       funchandles   One or several handles to type checking functions.
 OPT   nonstdname    Name of variable to use in error message. Default is
                     to use the function *inputname* to determine the
                     variable name.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rqre_datatype.m

SOURCE CODE ^

0001 % RQRE_DATATYPE   Checks if variable is of any expected data types
0002 %
0003 %    The input argument *funchandles* is a single or a cell array of handles to
0004 %    functions performing type checking, such as @isnumeric. For example, the
0005 %    following will generate an error message:
0006 %       a = 'a';
0007 %       rqre_datatype( a, {@isnumeric,@iscell} )
0008 %
0009 %    Issues an error if criterion not fulfilled. This error assumes that all
0010 %    functions are named as isxxxx, where xxxx is the name of the data type.
0011 %
0012 %    Use *<a href="matlab:help rqre_alltypes">rqre_alltypes</a>* to check if a combination of type checks is fullfilled.
0013 %
0014 % FORMAT   rqre_datatype( a, funchandles [,nonstdname] )
0015 %
0016 % IN    a             Variable to check.
0017 %       funchandles   One or several handles to type checking functions.
0018 % OPT   nonstdname    Name of variable to use in error message. Default is
0019 %                     to use the function *inputname* to determine the
0020 %                     variable name.
0021 
0022 % 2005-03-16   Created by Patrick Eriksson.
0023 
0024 
0025 function rqre_datatype( a, funchandles, nonstdname )
0026  
0027   
0028 %- A single function handle
0029 %
0030 if isa( funchandles, 'function_handle' )
0031   if funchandles(a)
0032     return                     % Return, a match has been found
0033   end
0034   % Repack for error message
0035   funchandles = { funchandles };  
0036 
0037   
0038 %- A cell array of function handles (?)
0039 %
0040 else
0041                                                                             %&%
0042   assert( all( cellfun( 'isclass', funchandles, 'function_handle' ) ), ...  %&%
0043         'The argument *funchandles* does not contain function handles.' );  %&%
0044   %                                                                         %&%
0045   if ~isvector( funchandles )                                               %&%
0046     error( 'The argument *funchandles* must be a vector (row or column).' );%&%
0047   end                                                                       %&%
0048 
0049   for i = 1:length(funchandles)
0050     if funchandles{i}(a)
0051       return                     % Return, a match has been found
0052     end
0053   end
0054 end
0055 
0056 
0057 %- Error messages
0058 %
0059 if nargin < 3
0060   vname = sprintf( 'The variable *%s*', inputname(1) );
0061 else
0062   assert( ischar( nonstdname )  &&  ~isempty( nonstdname ) );               %&%
0063   vname = nonstdname;
0064 end
0065 
0066 if length(funchandles) == 1
0067   s = func2str( funchandles{1} );
0068   error( '%s must be %s.', vname, s(3:end) );
0069 else
0070   fprintf('Accepted data types are\n');
0071   for i = 1:length(funchandles)
0072     s = func2str( funchandles{i} );
0073     fprintf('   %s\n', s(3:end) );
0074   end
0075   error( sprintf( '%s is not of any accepted types, listed above.', vname ) );
0076 end

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