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

  1. function [y,b] = istree(tr,ij)
  2.  
  3. % I=istree(X) returns I=1 if X is a tree (e.g, as created by the 
  4. %     function TREE); otherwise it returns I=0.
  5. %
  6. % [I,B]=ISTREE(X,PATH) returns I=1 if both X is a tree and PATH is
  7. %     a valid PATH in the tree X; otherwise it returns I=0.  If the
  8. %     optional output argument B is present, then B returns the value
  9. %     of the branch specified by PATH, provided X is a tree and PATH
  10. %     is a valid path in X. 
  11. %
  12.  
  13.  
  14. % R. Y. Chiang & M. G. Safonov 10/01/90    
  15. % Copyright (c) 1990 by the MathWorks, Inc.
  16. % All Rights Reserved.
  17. % ---------------------------------------------------------------------------
  18.  
  19. %  This function uses essentially the same code as BRANCH with only
  20. %  minor modification at beginning and end of code, and the replacement
  21. %  of ERROR with RETURN throughout
  22.  
  23. nin=nargin-1;
  24. nout=nargout;
  25. y=0;
  26. b=[];
  27. magic1 = 29816; % Every tree vector TR includes these two "magic numbers"
  28. magic2 = 18341; % in postions TR(2) and TR(3) for identification purposes.
  29.  
  30. if nin==-1, error('No input argument'),end
  31.  
  32. %  Check that TR is a tree
  33. msg='first input argument must be a TREE';
  34. %  Check that TR is a vector and has length at least 5
  35. if max(size(tr))<6 | min(size(tr))~=1,
  36.   % error(msg);
  37.   return
  38. end
  39. % If TR is a row vector, transpose it
  40. tr=tr(:);  
  41. % Check magic numbers to ensure that TR is a tree
  42. if tr(2)~=magic1 |tr(3)~=magic2
  43.   % error(msg);
  44.   return
  45. end
  46. if nin==0, y=1; return, end
  47.  
  48. % Now check whether IJ is a valid branch of TR
  49.  
  50. j=1;
  51.  
  52. % Start of part that is same as BRANCH.M, execept RETURN's replacing ERROR's
  53. %--------------------------------------------------------------------------
  54.    [rj,cj]=size(ij);
  55.    if rj~=1| cj==0,
  56.         % error('invalid path or index')
  57.         return
  58.    end
  59.    if isstr(ij),           % case of string-name path IJ=PATHJ
  60.       flag=1;
  61.       ij=[ij '/'];           % append extra '/' to string ij
  62.       if ij(1)~='/',ij=['/' ij];end  %if missing add leading '/'
  63.       pptrj=find(ij=='/');   % PPTRJ = pointers to /'s in PATH IJ
  64.       lj=max(size(pptrj))-1; % path length (number of names)
  65.    else                      % case of numeric path INDEX IJ
  66.       flag=0;             
  67.       lj=max(size(ij));      % path length (number of indices)
  68.    end
  69.    x = tr; 
  70.    % Now descend tree X one branch at a time, recursively
  71.    % overwriting X with root branch having index IJ(K)
  72.    for k=1:lj, 
  73.      % Check to make sure X is a TREE 
  74.      if max(size(x))<6 | min(size(x))~=1,
  75.        % error('Invalid path or branch');
  76.        return
  77.      end
  78.      if x(2)~=magic1 |x(3)~=magic2
  79.         % error('Invalid path or branch');
  80.         return
  81.      end
  82.  
  83.      [rx1,cx1]=size(x);
  84.      if flag ==1,
  85.         nm = branch(x,0);
  86.         nm=[',' nm ','];  % nm = ',name1,name2,..,namen,'
  87.  
  88.         % Now get k-th name in pathj
  89.         if pptrj(k+1)-pptrj(k)>1,
  90.             nmjk = ij(pptrj(k)+1:pptrj(k+1)-1);
  91.         else
  92.             % error('empty string encountered in path')
  93.             return
  94.         end
  95.  
  96.         if max(nmjk)<='9' & min(nmjk)>=0,  % If NMJK contains a string
  97.            [rnmjk,cnmjk]=size(nmjk);       % representation of an 
  98.            njk=0;                          % integer, set NJK equal
  99.            for jjj=1:cnmjk,                % to that integer
  100.                njk=nmjk(jjj)-'0'+10*njk;
  101.            end
  102.         else                               % otherwise...
  103.            % compare NMJK with names in NM and set 
  104.            % NJK = (position in nm of name matching NMJK); if no
  105.            % match found then set fflag = 0 and announce error
  106.            nmptr=find(nm==',');
  107.            nnm = max(size(nmptr))-1; % number branch names in nm
  108.            fflag=0;
  109.            iter=0;
  110.            while fflag==0 & iter<nnm
  111.               temp=nm(nmptr(iter+1)+1:nmptr(iter+2)-1);
  112.               if size(nmjk)==size(temp),    % if match found
  113.                  if nmjk==temp,             % set NJK=ITER+1
  114.                    njk=iter+1;              % and exit while 
  115.                    fflag=1;                 % loop 
  116.                  end                       
  117.               end                           
  118.               iter=iter+1;
  119.            end
  120.            if fflag==0,
  121.               % error(['branch ' ij(1:pptrj(k+1)-1) ' not present']);
  122.               return
  123.            end
  124.         end
  125.      else
  126.         njk = ij(k);
  127.      end      
  128.      msg=['branch B' num2str(j) ' index out of range'];
  129.      if njk>x(1)| njk<0 | njk+6>rx1,
  130.         % error(msg),
  131.         return
  132.      end
  133.      ptr1 = x(njk+6);   % points to start of njk-th subfield of DAT
  134.      if ptr1~=1,
  135.          rx = x(ptr1);  % rows of branch
  136.      else                   
  137.          rx=0;          % empty matrix is indicated by ptr1=1
  138.      end     
  139.      if rx<0,     % RX is negative if the branch contains string data 
  140.           rx = abs(x(ptr1)); 
  141.           sflag=1;
  142.      else
  143.           sflag=0;
  144.      end
  145.      if rx1>=ptr1+1,
  146.         cx = x(ptr1+1);   % cols of x
  147.      else
  148.         % error('Invalid path or branch')
  149.         return
  150.      end
  151.      lx = rx*cx;       % length of x
  152.      if lx>0 & ptr1+lx+1<=rx1,  % if new X empty and old X is big enough
  153.         xcol = x(ptr1+2:ptr1+lx+1); % Store new X data into XCOL
  154.         x = zeros(rx,cx);      % Create a new X matrix of correct dimension
  155.         x(:) = xcol(:);        % Now fill in new X with XCOL data
  156.      elseif lx==0,
  157.         x=[];
  158.      else
  159.         % error('invalid path or index')
  160.         return
  161.      end
  162.    end
  163.    if sflag==1,
  164.       x=setstr(x);  %set string bit
  165.    end
  166. % ------------End of part that is same as BRANCH.M  -----------------
  167.  
  168.    b=x;
  169.    y=1;
  170. % ------ End of  ISTREE.M --- RYC/MGS 10/25/90 %
  171.