Home > atmlab > handy > qcheck.m

qcheck

PURPOSE ^

QCHECK Ensures that a structure only contains recognised fields.

SYNOPSIS ^

function qcheck( qfun, Q )

DESCRIPTION ^

 QCHECK   Ensures that a structure only contains recognised fields.

    The function compares the fields of *Q* with the structure provided by
    *qfun*. The argument *qfun* shall be a pointer to a function 
    (e.g. @qdefault) that has a structure as first output argument and
    works without input arguments. This function should normally be a 
    function setting default values for all recognised fields. 

    This function is complemented with *qinfo*. If the functionality
    provided by these functions will be used, *qfun* shall have as
    second output argument an information structure. That is:
      [Q,INFO] = qdefaults;
    where the INFO structure shall have the same fields as Q, and each
    field of INFO holds a basic string with a description of the field.

    For a practical example, see e.g. the function *qarts*. Test e.g.
      more on
      qinfo(@qarts,'all');
    or 
      qinfo(@qarts,'W*');

 FORMAT   qcheck( qfun, Q )
        
 IN    qfun   Pointer to function providing default structure.
       Q      Structure to check.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

qcheck.m

SOURCE CODE ^

0001 % QCHECK   Ensures that a structure only contains recognised fields.
0002 %
0003 %    The function compares the fields of *Q* with the structure provided by
0004 %    *qfun*. The argument *qfun* shall be a pointer to a function
0005 %    (e.g. @qdefault) that has a structure as first output argument and
0006 %    works without input arguments. This function should normally be a
0007 %    function setting default values for all recognised fields.
0008 %
0009 %    This function is complemented with *qinfo*. If the functionality
0010 %    provided by these functions will be used, *qfun* shall have as
0011 %    second output argument an information structure. That is:
0012 %      [Q,INFO] = qdefaults;
0013 %    where the INFO structure shall have the same fields as Q, and each
0014 %    field of INFO holds a basic string with a description of the field.
0015 %
0016 %    For a practical example, see e.g. the function *qarts*. Test e.g.
0017 %      more on
0018 %      qinfo(@qarts,'all');
0019 %    or
0020 %      qinfo(@qarts,'W*');
0021 %
0022 % FORMAT   qcheck( qfun, Q )
0023 %
0024 % IN    qfun   Pointer to function providing default structure.
0025 %       Q      Structure to check.
0026 
0027 % 2004-09-07   Created by Patrick Eriksson.
0028 
0029 
0030 function qcheck( qfun, Q )
0031 
0032 
0033 Qref = feval( qfun );
0034 
0035 f1 = fieldnames( Q ); 
0036 f2 = fieldnames( Qref ); 
0037 
0038 ok = 1;
0039 
0040 for i = 1 : length(f2)
0041   if ~isfield( Q, f2{i} )
0042     fprintf('The field %s is missing.\n', f2{i} );
0043     ok = 0;
0044   end
0045 end
0046 
0047 
0048 if length(f1) ~= length(f2)  | ~ok
0049   for i = 1 : length(f1)
0050     if ~isfield( Qref, f1{i} )
0051       fprintf('The field %s is not defined.\n', f1{i} );
0052       ok = 0;
0053     end
0054   end
0055 end
0056 
0057 
0058 if ~ok
0059   fprintf('\n');
0060   error('See above.');
0061 end

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