image thumbnail

mklib: a pedestrian function library loader generator

by

 

05 May 2005 (Updated )

creates a function library loader from a list of m-files (possibly with subfunctions)

mkdemo
mkdemo
% MKLIB
% a pedestrian function library loader generator
%
% note the demo will create a file <goo.m> in this folder!
 
% assume some functions <flib?.m> are in this folder
          flst=dir('flib*.m');
 
% create the library loader <goo.m>
% ... see how it handles DUPLICATES
          lib=mklib('goo','flib1',{flst.name});
 
MKLIB> loader deleted!  F:\usr\matlab\tmp\fex\goo.m
MKLIB> +handle          F:\usr\matlab\tmp\fex\flib1.m > mysin
MKLIB> -nested          F:\usr\matlab\tmp\fex\flib1.m > nest_1
MKLIB> -nested          F:\usr\matlab\tmp\fex\flib1.m > nest_2
MKLIB> +handle          F:\usr\matlab\tmp\fex\flib1.m > mysum
MKLIB> -duplicate       flib1.m > mysin
MKLIB> -nested          flib1.m > nest_1
MKLIB> -nested          flib1.m > nest_2
MKLIB> -duplicate       flib1.m > mysum
MKLIB> +handle          flib2.m > fsin
MKLIB> +handle          flib2.m > fsum
MKLIB> no subfunctions  flib3.m
MKLIB> +handle          flib4.m > rsin
MKLIB> -nested          flib4.m > nest_1
MKLIB> -nested          flib4.m > nest_2
MKLIB> +handle          flib4.m > rsum
MKLIB> +handle          flib5.m > flib5
MKLIB> +handle          flib5.m > xsin
MKLIB> +handle          flib6.m > bsin
MKLIB> loader created   F:\usr\matlab\tmp\fex\goo.m
 
% assign the function handles
          fh=goo;
% ... or by
          fh=feval(lib)
 
fh = 
    mysin: @mysin
    mysum: @mysum
     fsin: @fsin
     fsum: @fsum
     rsin: @rsin
     rsum: @rsum
    flib5: @flib5
     xsin: @xsin
     bsin: @bsin
 
% use subfunction         flib1 > MYSIN
          fh.mysin(0:30:90)
 
ans =  0          0.5      0.86603            1
 
% create an alias for     flib1 > MYSUM
          fa=fh.mysum;
          fa([30 60])
 
ans = 90
 
% look at the handle's contents
          functions(fa)
 
ans = 
     function: 'mysum'
         type: 'scopedfunction'
         file: 'F:\usr\matlab\tmp\fex\goo.m'
    parentage: {'mysum'  'goo'}
 
% show auto-generated help of the library loader
% note NESTED and DUPLICATE subfunctions are
% - listed with a negative enumerator
% - NOT assigned
          goo            % -or- help(lib)
 
  MAKELIB      :      a function library loader generator
  version      :      05-Jan-2006 17:45:18               
  ML release   :      7.1.0.246 (R14) Service Pack 3     
  -------------                                     
  OUTPUT       >                                    
  library      :      F:\usr\matlab\tmp\fex\goo.m        
  created      :      05-Jan-2006 17:45:23               
  -------------                                     
  INPUT        <                                    
  files        :      7                              number of files used
  routines     :      37                             all function calls found
   subroutines :      17      [SUB: 11 / NST: 6]     all functions found:         (subroutines / +nested)
    assigned   :      9       [SUB: 11 / DUP: 2]     all functions assigned:      (subroutines / -duplicates)
   other       :      20                             all other calls found:       (system/built-in/...)
  lines        :      66                             total length of files used
  -------------                                     
  file       1 :      F:\usr\matlab\tmp\fex\flib1.m      
   subroutines :                     2 [SUB 4: NST -2/DUP -0]         
      1:     1 =      +handle        mysin                     
     -1:    -1 =      -nested        nest_1                    
     -2:    -2 =      -nested        nest_2                    
      2:     2 =      +handle        mysum                     
  file       2 :      flib1.m                            
   subroutines :                     0 [SUB 4: NST -2/DUP -2]         
     -3:    -1 !      -duplicate     mysin                  
     -3:    -1 =      -nested        nest_1                    
     -4:    -2 =      -nested        nest_2                    
     -4:    -2 !      -duplicate     mysum                  
  file       3 :      flib2.m                            
   subroutines :                     2 [SUB 2: NST -0/DUP -0]         
      5:     1 =      +handle        fsin                      
      6:     2 =      +handle        fsum                      
  file       4 :      flib3.m                            
   subroutines :      0              script?                         
  file       5 :      flib4.m                            
   subroutines :                     2 [SUB 4: NST -2/DUP -0]         
      7:     1 =      +handle        rsin                      
     -5:    -1 =      -nested        nest_1                    
     -6:    -2 =      -nested        nest_2                    
      8:     2 =      +handle        rsum                      
  file       6 :      flib5.m                            
   subroutines :                     2 [SUB 2: NST -0/DUP -0]         
      9:     1 =      +handle        flib5                     
     10:     2 =      +handle        xsin                      
  file       7 :      flib6.m                            
   subroutines :                     1 [SUB 1: NST -0/DUP -0]         
     11:     1 =      +handle        bsin                      
 
% look at <goo.m>
% note BLOCK COMMENTING of
% - scripts
% - files that contain at least one DUPLICATE
          type goo       % -or- type(lib)
 
% MAKELIB      :      a function library loader generator
% version      :      05-Jan-2006 17:45:18               
% ML release   :      7.1.0.246 (R14) Service Pack 3     
% -------------                                     
% OUTPUT       >                                    
% library      :      F:\usr\matlab\tmp\fex\goo.m        
% created      :      05-Jan-2006 17:45:23               
% -------------                                     
% INPUT        <                                    
% files        :      7                                  
% routines     :      37                                 
%  subroutines :      17      [SUB: 11 / NST: 6]              
%   assigned   :      9       [SUB: 11 / DUP: 2]               
%  other       :      20                                 
% lines        :      66                                 
% -------------                                     
% file       1 :      F:\usr\matlab\tmp\fex\flib1.m      
%  subroutines :                     2 [SUB 4: NST -2/DUP -0]         
%     1:     1 =      +handle        mysin                     
%    -1:    -1 =      -nested        nest_1                    
%    -2:    -2 =      -nested        nest_2                    
%     2:     2 =      +handle        mysum                     
% file       2 :      flib1.m                            
%  subroutines :                     0 [SUB 4: NST -2/DUP -2]         
%    -3:    -1 !      -duplicate     mysin                  
%    -3:    -1 =      -nested        nest_1                    
%    -4:    -2 =      -nested        nest_2                    
%    -4:    -2 !      -duplicate     mysum                  
% file       3 :      flib2.m                            
%  subroutines :                     2 [SUB 2: NST -0/DUP -0]         
%     5:     1 =      +handle        fsin                      
%     6:     2 =      +handle        fsum                      
% file       4 :      flib3.m                            
%  subroutines :      0              script?                         
% file       5 :      flib4.m                            
%  subroutines :                     2 [SUB 4: NST -2/DUP -0]         
%     7:     1 =      +handle        rsin                      
%    -5:    -1 =      -nested        nest_1                    
%    -6:    -2 =      -nested        nest_2                    
%     8:     2 =      +handle        rsum                      
% file       6 :      flib5.m                            
%  subroutines :                     2 [SUB 2: NST -0/DUP -0]         
%     9:     1 =      +handle        flib5                     
%    10:     2 =      +handle        xsin                      
% file       7 :      flib6.m                            
%  subroutines :                     1 [SUB 1: NST -0/DUP -0]         
%    11:     1 =      +handle        bsin                      
 
function      fh=goo(varargin)                           
      if      ~nargout                                        
              help(mfilename);                                  
      else                                               
              fh=[];                                            
              fh.mysin=@mysin;                                  
              fh.mysum=@mysum;                                  
              fh.fsin=@fsin;                                    
              fh.fsum=@fsum;                                    
              fh.rsin=@rsin;                                    
              fh.rsum=@rsum;                                    
              fh.flib5=@flib5;                                  
              fh.xsin=@xsin;                                    
              fh.bsin=@bsin;                                    
      end                                                
              end                                               
 
% F:\usr\matlab\tmp\fex\flib1.m                     
% -----------------------------                     
function      r=mysin(v)                                 
                             f=@(r) r;                                       
                             r=f(v);                                         
                             r=nest_1;                                       
                             r=sin(r);                                       
      function               rr=nest_1                                
                             rr=v*pi;                                        
                             rr=nest_2;                                      
              function       rx=nest_2                                
                             rx=rr/180;                                      
              end                                               
      end                                                
end                                                 
function      r=mysum(v)                                 
              r=sum(v);                                       
end                                                 
% flib1.m                                           
% -------                                           
%{                                                  
function      r=mysin(v)                                 
                             f=@(r) r;                                       
                             r=f(v);                                         
                             r=nest_1;                                       
                             r=sin(r);                                       
      function               rr=nest_1                                
                             rr=v*pi;                                        
                             rr=nest_2;                                      
              function       rx=nest_2                                
                             rx=rr/180;                                      
              end                                               
      end                                                
end                                                 
function      r=mysum(v)                                 
              r=sum(v);                                       
end                                                 
%}                                                  
% flib2.m                                           
% -------                                           
function      r=fsin(v)                                  
              r=sind(v);                                        
end                                                 
function      r=fsum(v)                                  
              r=sum(v);                                         
end                                                 
% flib3.m                                           
% -------                                           
%{                                                  
% a script                                          
disp('flib3');                                      
%}                                                  
% flib4.m                                           
% -------                                           
function      r=rsin(v)                                  
                             f=@(r) r;                                       
                             r=f(v);                                         
                             r=nest_1;                                       
                             r=sin(r);                                       
      function               rr=nest_1                                
                             rr=v*pi;                                        
                             rr=nest_2;                                      
              function       rx=nest_2                                
                             rx=rr/180;                                      
              end                                               
      end                                                
end                                                 
function      r=rsum(v)                                  
              r=sum(v);                                       
end                                                 
% flib5.m                                           
% -------                                           
function      r=flib5(v)                                 
              f=@xsin;                                          
              r=f(v);                                           
end                                                 
function      r=xsin(v)                                  
              r=bsin(v);                                        
end                                                 
% flib6.m                                           
% -------                                           
function      r=bsin(v)                                  
              r=sind(v);                                        
end                                                 

Contact us