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

  1. function tr = tree(nm,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,...
  2.   b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,...
  3.   b31,b32,b33,b34,b35,b36,b37,b38,b39,b40,b41,b42,b43,b44,b45,b46,...
  4.   b47,b48,b49,b50)
  5. % TR = TREE(NM,B1,B2,...,BN) creates a hierarhical data structure TR 
  6. %     called a "tree"  consisting of a single column vector into which
  7. %     is encoded all the data and dimension information from its
  8. %     branches B1, B2,...,BN along with a numerical index and a string-
  9. %     name for each branch. The input argument NM assigns names to the
  10. %     branches; it must be of the form
  11. %           NM = 'name1,name2,...,nameN'
  12. %     The names may be any valid MATLAB variable names and must be
  13. %     separated by commas; if no names are to be assigned, set NM=''.
  14. %     The arguments B1,B2,...,BN (called the "root branches" of the
  15. %     tree) may be matrices, vectors, strings, or trees themselves.
  16. %     Related functions include the following:
  17. %         BRANCH   returns branches of a tree
  18. %         ISTREE   tests existence of a specified branch
  19. %         BRANCH(TR,0)  returns NM
  20. %         TR(1)    returns the number N of root branches
  21. %
  22.  
  23. % R. Y. Chiang & M. G. Safonov 10/01/90    
  24. % Copyright (c) 1990 by the MathWorks, Inc.
  25. % All Rights Reserved.
  26. % ---------------------------------------------------------------------------
  27.  
  28. if nargin==0,error('missing input arguments');end
  29.  
  30. % Initialize variables
  31. n = nargin-1;       % number of branches N
  32. ind = 7+n;          % point to beginning of B0 data subfield of DAT
  33. dat = [];
  34. ptr = [];
  35. magic1 = 29816; 
  36. magic2 = 18341;
  37.  
  38. % create header for tree
  39. header = [n;magic1;magic2;0;0];
  40. % Note:  The tree TR has the "magic numbers" 29816 and 18341 in
  41. % positions 2 and 3 , respectively.  These magic numbers are used 
  42. % by other functions to distinguish a tree from an ordinary vector
  43. % The two zero entries in positions 4 and 5 have no significance
  44. % other than as place holders; they may be overwritten with data
  45. % for specialized applications.
  46.  
  47. msg = 'first input argument must be string of names separated by commas';
  48. [rnm,cnm]=size(nm);
  49. if rnm>1,error(msg),end
  50. if rnm==1 & ~isstr(nm),error(msg),end
  51.  
  52. % Remove any spaces from NM
  53. nm=[nm ' ']; nm(find(nm==' '))='';
  54. b0 = nm;                  % Let NM be branch zero of the tree
  55.  
  56. % Now check names in NM for validity
  57. nm1=[',' nm ','];
  58. ind1=find(nm1==',');
  59. c=max(size(ind1))-1;
  60. if c~=n & min(size(nm))>0,
  61.    error(['Number of names in string ' 13 10 '''' nm '''' 13 10 'is not equal to number of branches of tree'])
  62. end
  63. for i=1:c,
  64.    nmi=nm1(ind1(i)+1:ind1(i+1)-1);
  65.    temp=find((nmi>='0' & nmi<='9')|(nmi>='A' & nmi<='Z')|nmi=='_'|(nmi>='a' & nmi<='z'));
  66.    if min(size(nmi))>0
  67.       temp=nmi(temp);
  68.       if ~issame(temp,nmi) | ~(nmi(1)>=65),
  69.          error([' ' nmi ' is not a valid branch name.'])
  70.       end
  71.    end
  72. end
  73.  
  74. % Now create DAT vector with data on B0, B1, B2 et cetera and a PTR vector
  75. % containing pointers pointing to the beginnning of the subfields of
  76. % DAT corresponding to each of B0,B1, etc.
  77.  
  78. % Create a subfield of DAT for each of the branches B0, B1, B2, etc.
  79. for i = 0:n,
  80.    eval(['b = b' num2str(i) ';'])   % B = i-th branch
  81.    [rb,cb]=size(b);
  82.    if cb>0,
  83.       ptr = [ptr;ind];
  84.       % if B is string, set rb=-rb,end  % minus flags string branch 
  85.       if isstr(b(:)'),
  86.          rb=-rb;    % string data marked by negative sign
  87.       end                   
  88.       dat = [dat;rb;cb;b(:)]; % Append Bdat to DAT field
  89.  
  90.       ind=ind+size(b(:))*[1;0]+2;
  91.            % IND points to start of data field for branch i+1
  92.    else
  93.        ptr = [ptr;1];      % PTR for empty matrix is 1
  94.    end
  95. end
  96. % PTR vector now has length N+1 with first entry pointing to start
  97. % of the NM field (i.e., B0) and the remaining N entries pointing
  98. % to the start of the fields containing the data for B1,B2,...,BN
  99.  
  100. % Now assemble the tree vector TR
  101. % Format:
  102. % index     1        6       PTR(0)  ...      PTR(N)
  103.       %     HEADER   PTR     B0dat   ...      BNdat ];
  104. tr = 1*[header; ptr; dat]; 
  105. % Note:  Multiplying TR by 1 unsets string bit, if needed
  106.  
  107. % ------ End of TREE.M --- RYC/MGS 10/05/90 %
  108.