home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXYZ.DI$ / CLABEL.M next >
Encoding:
Text File  |  1993-03-07  |  4.0 KB  |  136 lines

  1. function clabel(cs, contours)
  2. %CLABEL    Add contour labels to a contour plot.
  3. %    CLABEL(CS) adds height labels to the current contour plot
  4. %    using the contour structure CS output from the CONTOUR routine.
  5. %    The label positions are selected randomly.  For example:
  6. %
  7. %       cs = contour(rand(10)); clabel(cs)
  8. %
  9. %    CLABEL(CS,V) labels just those contour levels given in
  10. %    vector V.  The default action is to label all known contours.
  11. %
  12. %    CLABEL(CS,'manual') places contour labels at the locations
  13. %    clicked on with a mouse.  Pressing the return key terminates
  14. %    labeling.  Use the space bar to enter contours and the arrow
  15. %    keys to move the crosshair if no mouse is available.
  16. %
  17. %    See also CONTOUR, GINPUT.
  18.  
  19. %    Charles R. Denham, MathWorks, 1988, 1989.
  20. %    Revised 4/12/90 by CRD: XYLIST now in screen coordinates.
  21. %    Revised 4/16/90 by CRD: Removed an inappropriate test.
  22. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  23.  
  24. if nargin == 0
  25.     error('Not enough input arguments.')
  26. end
  27. if min(size(cs)) > 2
  28.     error('First input must be a valid contour description matrix.')
  29. end
  30. cax = gca;
  31. next = lower(get(cax,'NextPlot'));
  32. hold_state = ishold;
  33. manual = 0;
  34. choice = 0;
  35. if nargin > 1
  36.    if isstr(contours)
  37.       manual = strcmp(contours, 'manual');
  38.     if ~manual
  39.         error('Invalid argument.');
  40.     end
  41.      else
  42.     choice = 1;
  43.     contours = sort(contours(:));
  44.    end
  45. end
  46.  
  47. hold on;
  48. [mcs, ncs] = size(cs);
  49.  
  50. % Decompose contour data structure if manual mode.
  51.  
  52. if manual
  53.    disp(' '), disp('    Please wait a moment...')
  54.    x = []; y = []; clist = []; k = 0; n = 0;
  55.    while (1)
  56.       k = k + n + 1; if k > ncs, break, end
  57.       c = cs(1,k); n = cs(2,k); nn = 2 .* n -1;
  58.       xtemp = zeros(nn, 1); ytemp = zeros(nn, 1);
  59.       xtemp(1:2:nn) = cs(1, k+1:k+n);
  60.       xtemp(2:2:nn) = (xtemp(1:2:nn-2) + xtemp(3:2:nn)) ./ 2;
  61.       ytemp(1:2:nn) = cs(2, k+1:k+n);
  62.       ytemp(2:2:nn) = (ytemp(1:2:nn-2) + ytemp(3:2:nn)) ./ 2;
  63.       x = [x; xtemp]; y = [y; ytemp];   % Keep these.
  64.       clist = [clist; c .* ones(2*n-1, 1)];
  65.    end
  66.    ax = axis; 
  67.    xmin = ax(1); xmax = ax(2); ymin = ax(3); ymax = ax(4);
  68.    xrange = xmax - xmin; yrange = ymax - ymin;
  69.    xylist = (x .* yrange + sqrt(-1) .* y .* xrange);
  70.    disp(' ');
  71.    disp('   Carefully select contours for labeling.')
  72.    disp('   When done, press RETURN while the Graph window is the active window.')
  73. end
  74.  
  75. k = 0; n = 0; flip = 0;
  76.  
  77. ENT = 3;   % Ascii for "ENTER" key.
  78. CR = 13;   % Ascii for "RETURN" key.
  79.  
  80. while (1)
  81.  
  82. % Use GINPUT and select nearest point if manual.
  83.  
  84.    if manual
  85.       [xx, yy, button] = ginput(1);
  86.       if isempty(button), break, end
  87.       if button == ENT | button == CR, break, end
  88.       if xx < xmin | xx > xmax, break, end
  89.       if yy < ymin | yy > ymax, break, end
  90.       xy = xx .* yrange + sqrt(-1) .* yy .* xrange;
  91.       dist = abs(xylist - xy);
  92.       f = find(dist == min(dist));
  93.       if length(f) > 0
  94.          f = f(1); xx = x(f); yy = y(f); c = clist(f);
  95.          okay = 1;
  96.         else
  97.          okay = 0;
  98.       end
  99.    end
  100.  
  101. % Select a labeling point randomly if not manual.
  102.  
  103.    if ~manual
  104.       k = k + n + 1; if k > ncs, break, end
  105.       c = cs(1, k); n = cs(2, k);
  106.       if choice
  107.          f = find(abs(c-contours)/max(eps+abs(contours)) < .00001);
  108.          okay = length(f) > 0;
  109.         else
  110.          okay = 1;
  111.       end
  112.       if okay
  113. % put next line in in case rand('normal') is set - and to avoid
  114. % obsolescence message
  115.          r = rand; while (r > 1 | r < 0) r = rand; end
  116.          j = fix(r.* (n - 1)) + 1;
  117.          if flip, j = n - j; end
  118.          flip = ~flip;
  119.          x1 = cs(1, j+k); y1 = cs(2, j+k);
  120.          x2 = cs(1, j+k+1); y2 = cs(2, j+k+1);
  121.          xx = (x1 + x2) ./ 2; yy = (y1 + y2) ./ 2;   % Test was here; removed.
  122.       end
  123.    end
  124.  
  125. % Label the point.
  126.  
  127.    if okay
  128.       s = [' ' sprintf('%0.3g', c)];
  129.       plot(xx, yy, '+');
  130.     text(xx, yy, s, 'verticalalignment', 'bottom', 'horizontalalignment', 'left');
  131.    end
  132.  
  133. end
  134. if ~hold_state, set(cax,'NextPlot',next); end
  135.  
  136.