home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 7.ddi / ROBUST.DI$ / BRANCH.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  6.7 KB  |  190 lines

  1. function [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20] = branch(tr,j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20)
  2.  
  3. % [B1,B2,...,BN] = BRANCH(TR,PATH1,PATH2,...,PATHN) returns N sub-
  4. %     branches of tree TR.  If NARGIN==1, the root branches are returned
  5. %     in sequence by numerical index; otherwise, the branches returned
  6. %     are determined by the paths PATH1, PATH2,...,PATHN.  Each path is
  7. %     normally a STRING of the form 
  8. %           PATH = '/name1/name2/.../namen'
  9. %     where name1,name2, et cetera are the string names of the branches
  10. %     which define the path from the tree root to the sub-branch of 
  11. %     interest.  Alternatively, one may substitute for any PATH a row
  12. %     vector containing the integer indices of the branches which define
  13. %     the PATH.  For example, if S=TREE('a,b,c','foo',[49 50],'bar') then 
  14. %     BRANCH(S,'c') and BRANCH(S,3) both return the value 'bar'.
  15. %
  16. %  See also ISTREE and TREE. 
  17.  
  18. %
  19. % R. Y. Chiang & M. G. Safonov 10/01/90    
  20. % Copyright (c) 1990 by the MathWorks, Inc.
  21. % All Rights Reserved.
  22. % ---------------------------------------------------------------------------
  23. nin = nargin-1;
  24. n=max([nargout,1]);  
  25. %    
  26. if nin==-1,error('no tree input specified'),end
  27.  
  28. magic1 = 29816; % Every tree vector TR includes these two "magic numbers"
  29. magic2 = 18341; % in postions TR(2) and TR(3) for identification purposes.
  30.  
  31. %  Check that TR is a tree
  32. msg='first input argument must be a TREE';
  33. %  Check that TR is a vector and has length at least 5
  34. if max(size(tr))<6 | min(size(tr))~=1,
  35.   error(msg);
  36. end
  37. % If TR is a row vector, transpose it
  38. tr=tr(:);  
  39. % Check magic numbers to ensure that TR is a tree
  40. if tr(2)~=magic1 |tr(3)~=magic2
  41.   error(msg);
  42. end
  43.  
  44. % Now get the paths I1,...,IN:
  45. % If no paths specified, assign values I1=1,...,IN=n; otherwise
  46. % copy Ij's from Jj's, breaking up string Jj's at commas.
  47. if nin==0,
  48.    for j=1:n,
  49.       temp=['i' num2str(j) '=' num2str(j) ';'];
  50.       eval(temp);
  51.    end
  52.    iind=n;
  53. end
  54. if nin>0,
  55.   iind=0;
  56.   for jind=1:nin,
  57.      eval(['jj=j' num2str(jind) ';']),
  58.      if isstr(jj),             % If Jj is a string, create one
  59.         jj=[',' jj ','];       % extra Ij for each comma in Jj
  60.         ind=find(jj==',');
  61.         [rind,cind]=size(ind);
  62.         for j=1:cind-1,  
  63.             iind=iind+1;
  64.             ptr1=ind(j)+1;
  65.             ptr2=ind(j+1)-1;
  66.             eval(['i' num2str(iind) '=jj(ptr1:ptr2);']),
  67.         end         
  68.      else                      % otherwise, simply copy Jj onto Ij
  69.         iind=iind+1;
  70.         eval(['i' num2str(iind) '=jj;'])         
  71.      end
  72.   end
  73. end
  74. nin=iind;
  75.  
  76. % Number of paths must not be less the NARGOUT
  77. if n>nin, 
  78.    msg ='Too many output arguments.';
  79.    error(msg),
  80. end
  81.  
  82. %  For each J=1,...,N follow path IJ to get branch BJ
  83. for j = 1:n,
  84.    temp =['ij = i' num2str(j) ';'];  % IJ = Ij
  85.    eval(temp);
  86.    [rj,cj]=size(ij);
  87.    if rj~=1 | cj==0,error('invalid path or index'),end
  88.    if isstr(ij),           % case of string-name path IJ=PATHJ
  89.       flag=1;
  90.       ij=[ij '/'];           % append extra '/' to string ij
  91.       if ij(1)~='/',ij=['/' ij];end  %if missing add leading '/'
  92.       pptrj=find(ij=='/');   % PPTRJ = pointers to /'s in PATH IJ
  93.       lj=max(size(pptrj))-1; % path length (number of names)
  94.    else                      % case of numeric path INDEX IJ
  95.       flag=0;             
  96.       lj=max(size(ij));      % path length (number of indices)
  97.    end
  98.    x = tr; 
  99.    % Now descend tree X one branch at a time, recursively
  100.    % overwriting X with root branch having index IJ(K)
  101.    for k=1:lj, 
  102.      % Check to make sure X is a TREE 
  103.      if max(size(x))<6 | min(size(x))~=1,
  104.        error('Invalid path or branch');
  105.      end
  106.      if x(2)~=magic1 |x(3)~=magic2
  107.         error('Invalid path or branch');
  108.      end
  109.  
  110.      [rx1,cx1]=size(x);
  111.      if flag ==1,
  112.         nm = branch(x,0);
  113.         nm=[',' nm ','];  % nm = ',name1,name2,..,namen,'
  114.  
  115.         % Now get k-th name in pathj
  116.         if pptrj(k+1)-pptrj(k)>1,
  117.             nmjk = ij(pptrj(k)+1:pptrj(k+1)-1);
  118.         else
  119.             error('empty string encountered in path')
  120.         end
  121.  
  122.         if max(nmjk)<='9' & min(nmjk)>=0,  % If NMJK contains a string
  123.            [rnmjk,cnmjk]=size(nmjk);       % representation of an 
  124.            njk=0;                          % integer, set NJK equal
  125.            for jjj=1:cnmjk,                % to that integer
  126.                njk=nmjk(jjj)-'0'+10*njk;
  127.            end
  128.         else                               % otherwise...
  129.            % compare NMJK with names in NM and set 
  130.            % NJK = (position in nm of name matching NMJK); if no
  131.            % match found then set fflag = 0 and announce error
  132.            nmptr=find(nm==',');
  133.            nnm = max(size(nmptr))-1; % number branch names in nm
  134.            fflag=0;
  135.            iter=0;
  136.            while fflag==0 & iter<nnm
  137.               temp=nm(nmptr(iter+1)+1:nmptr(iter+2)-1);
  138.               if size(nmjk)==size(temp),    % if match found
  139.                  if nmjk==temp,             % set NJK=ITER+1
  140.                    njk=iter+1;              % and exit while 
  141.                    fflag=1;                 % loop 
  142.                  end                       
  143.               end                           
  144.               iter=iter+1;
  145.            end
  146.            if fflag==0,
  147.               error(['branch ' ij(1:pptrj(k+1)-1) ' not present']);
  148.            end
  149.         end
  150.      else
  151.         njk = ij(k);
  152.      end      
  153.      msg=['branch B' num2str(j) ' index out of range'];
  154.      if njk>x(1)| njk<0 | njk+6>rx1, error(msg),end
  155.      ptr1 = x(njk+6);   % points to start of njk-th subfield of DAT
  156.      if ptr1~=1,
  157.          rx = x(ptr1);  % rows of branch
  158.      else                   
  159.          rx=0;          % empty matrix is indicated by ptr1=1
  160.      end     
  161.      if rx<0,     % RX is negative if the branch contains string data 
  162.           rx = abs(x(ptr1)); 
  163.           sflag=1;
  164.      else
  165.           sflag=0;
  166.      end
  167.      if rx1>=ptr1+1,
  168.         cx = x(ptr1+1);   % cols of x
  169.      else
  170.         error('Invalid path or branch')
  171.      end
  172.      lx = rx*cx;       % length of x
  173.      if lx>0 & ptr1+lx+1<=rx1,  % if new X empty and old X is big enough
  174.         xcol = x(ptr1+2:ptr1+lx+1); % Store new X data into XCOL
  175.         x = zeros(rx,cx);      % Create a new X matrix of correct dimension
  176.         x(:) = xcol(:);        % Now fill in new X with XCOL data
  177.      elseif lx==0,
  178.         x=[];
  179.      else
  180.         error('invalid path or index')
  181.      end
  182.    end
  183.    if sflag==1,
  184.       x=setstr(x);  %set string bit
  185.    end
  186.    temp=['b' num2str(j) ' = x;'];
  187.    eval(temp);
  188. end
  189. % ------ End of BRANCH.M --- RYC/MGS 10/05/90 %
  190.