Home > atmlab > handy > qinfo.m

qinfo

PURPOSE ^

QINFO Prints Q structure information

SYNOPSIS ^

function qinfo( qfun, field, fid, lines )

DESCRIPTION ^

 QINFO   Prints Q structure information 

    This function prints field information for a "Q-structure" if the
    general format described in *qcheck* is followed. 

    The information text shall be a plain string. Formatting of the output
    is handled by the function, where position of row brakes is determined
    by the Atmlab setting SCREEN_WIDTH. Row brakes can further be hard 
    coded and are then indicated by the character '#'. Leave some space
    after '#' if you want the new paragraph to be intended.

    Some examples:
    Assuming that *qdef* provides needed information, information for 
    field DUMMY is obtained by:
       qinfo(@qdef,'DUMMY');
    A trailing wild character can also be used:
       qinfo(@qdef,'NEW*');
    To create a file qdoc.txt with information for all fields:
       qinfo(@qdef,'all',''qdoc.txt');

 FORMAT   qinfo( qfun [, field, fid, lines ] )
        
 IN    qfun   Pointer to function providing default structure.
 OPT   field  Field name, or 'all'. Default is all.
       fid    File identifier(s) for output. Or name on file to create.
              Default is to only print on screen.
       lines  As the same argument for *out*.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

qinfo.m

SOURCE CODE ^

0001 % QINFO   Prints Q structure information
0002 %
0003 %    This function prints field information for a "Q-structure" if the
0004 %    general format described in *qcheck* is followed.
0005 %
0006 %    The information text shall be a plain string. Formatting of the output
0007 %    is handled by the function, where position of row brakes is determined
0008 %    by the Atmlab setting SCREEN_WIDTH. Row brakes can further be hard
0009 %    coded and are then indicated by the character '#'. Leave some space
0010 %    after '#' if you want the new paragraph to be intended.
0011 %
0012 %    Some examples:
0013 %    Assuming that *qdef* provides needed information, information for
0014 %    field DUMMY is obtained by:
0015 %       qinfo(@qdef,'DUMMY');
0016 %    A trailing wild character can also be used:
0017 %       qinfo(@qdef,'NEW*');
0018 %    To create a file qdoc.txt with information for all fields:
0019 %       qinfo(@qdef,'all',''qdoc.txt');
0020 %
0021 % FORMAT   qinfo( qfun [, field, fid, lines ] )
0022 %
0023 % IN    qfun   Pointer to function providing default structure.
0024 % OPT   field  Field name, or 'all'. Default is all.
0025 %       fid    File identifier(s) for output. Or name on file to create.
0026 %              Default is to only print on screen.
0027 %       lines  As the same argument for *out*.
0028 
0029 % 2004-09-07   Created by Patrick Eriksson.
0030 
0031 function qinfo( qfun, field, fid, lines )
0032 %
0033 if nargin < 2
0034   field = 'all';
0035 end
0036 if nargin < 3
0037   fid = 1;
0038 end
0039 if nargin < 4
0040   lines = true;
0041 end
0042 
0043 if ischar(fid)
0044   fid = fileopen( fid, 'w' );
0045   filetoclose = 1;
0046 else
0047   filetoclose = 0;
0048 end
0049 
0050 
0051 [Q,INFO] = feval( qfun );
0052 
0053 
0054 f1 = fieldnames( Q ); 
0055 f2 = fieldnames( INFO ); 
0056 
0057 ok = 1;
0058 
0059 for i = 1 : length(f1)
0060   if ~isfield( INFO, f1{i} )
0061     fprintf('The Q-field %s is not found in INFO.\n', f1{i} );
0062     ok = 0;
0063   end
0064 end
0065 
0066 
0067 if length(f1) ~= length(f2)  | ~ok
0068   for i = 1 : length(f2)
0069     if ~isfield( Q, f2{i} )
0070       fprintf('The INFO-field %s is not defined in Q.\n', f1{i} );
0071       ok = 0;
0072     end
0073   end
0074 end
0075 
0076 
0077 if ~ok
0078   fprintf('\n');
0079   error('Inconsistency found when comparing Q and INFO.');
0080 end
0081 
0082 
0083 if strcmp( field, 'all' )  |  strcmp( tail(field,1), '*' )
0084 
0085   fields = fieldnames( INFO );
0086 
0087   if strcmp( tail(field,1), '*' )
0088     n   = length( field ) - 1;
0089     ind = find( strncmp( field(1:n), fields, n ) );
0090     if isempty(ind)
0091       fprintf('No fields match the search string.\n');
0092       return
0093     else
0094       fields = fields( ind );
0095     end
0096   end
0097 
0098   out(0,1,fid,lines);
0099   for i = 1 : length(fields)  
0100     printfield( fields{i}, getfield( INFO, fields{i} ), fid, lines );
0101     if i < length(fields)
0102       out(0,0,fid,lines);
0103     end
0104   end
0105   out(0,-1,fid,lines);
0106 
0107 
0108 else
0109 
0110   if ~isfield( INFO, field )
0111     error(sprintf('The field %s is not recognised.', field));
0112   end
0113 
0114   s = getfield( INFO, field );
0115 
0116   if ~ischar( s )
0117     error(sprintf('Information for field %s is not a string.', field));
0118   end
0119 
0120   out(0,1,fid,lines);
0121   printfield( field, s, fid, lines );
0122   out(0,-1,fid,lines);
0123 
0124 end
0125 
0126 
0127 if  filetoclose 
0128   fileclose( fid );
0129 end
0130 
0131 
0132 
0133 %----------------------------------------------------------------------------
0134 
0135 function printfield( field, s, fid, lines )
0136   %
0137   atmlab( 'require', {'SCREEN_WIDTH'} );
0138   ncols = atmlab( 'SCREEN_WIDTH' ) - 4;
0139   ls = length( s );
0140   %
0141   ind  = [find( s == ' ' ) ls+1 ];
0142   pind = find( s == '#' );
0143   ready = 0;
0144   i = 1;
0145   %
0146   out(0,sprintf('%s:',field),fid,lines);
0147   %out(0,'-',fid,lines);
0148   while ~ready
0149     j = ind(max(find( ind <= i+ncols )))-1;
0150     if j <= i + 2    % Fix to handle cases when SCREEN_WIDTH is smaller than
0151       j  = min( [ i+ncols-1 length(s) ] );  % maximum length of a "word"
0152       jj = 1;
0153     else
0154       jj = 2;
0155     end
0156     if any( pind <= j )
0157       j = min(pind) - 1;
0158       pind = tail( pind, length(pind)-1 );
0159     end
0160     out(0,s(i:j),fid,lines);
0161     i = j + jj;
0162     if i > ls
0163       ready = 1;
0164     end
0165   end
0166 return
0167 
0168

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