home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXY.DI$ / POLAR.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.8 KB  |  129 lines

  1. function hpol = polar(theta,rho,line_style)
  2. %POLAR    Polar coordinate plot.
  3. %    POLAR(THETA, RHO) makes a plot using polar coordinates of
  4. %    the angle THETA, in radians, versus the radius RHO.
  5. %    POLAR(THETA,RHO,S) uses the linestyle specified in string S.
  6. %    See PLOT for a description of legal linestyles.
  7. %
  8. %    See also PLOT, LOGLOG, SEMILOGX, SEMILOGY.
  9.  
  10. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  11.  
  12. if nargin < 1
  13.     error('Requires 2 or 3 input arguments.')
  14. elseif nargin == 2 
  15.     if isstr(rho)
  16.         line_style = rho;
  17.         rho = theta;
  18.         [mr,nr] = size(rho);
  19.         if mr == 1
  20.             theta = 1:nr;
  21.         else
  22.             th = (1:mr)';
  23.             theta = th(:,ones(1,nr));
  24.         end
  25.     else
  26.         line_style = 'auto';
  27.     end
  28. elseif nargin == 1
  29.     line_style = 'auto';
  30.     rho = theta;
  31.     [mr,nr] = size(rho);
  32.     if mr == 1
  33.         theta = 1:nr;
  34.     else
  35.         th = (1:mr)';
  36.         theta = th(:,ones(1,nr));
  37.     end
  38. end
  39. if isstr(theta) | isstr(rho)
  40.     error('Input arguments must be numeric.');
  41. end
  42. if any(size(theta) ~= size(rho))
  43.     error('THETA and RHO must be the same size.');
  44. end
  45.  
  46. % get hold state
  47. cax = newplot;
  48. next = lower(get(cax,'NextPlot'));
  49. hold_state = ishold;
  50.  
  51. % get x-axis text color so grid is in same color
  52. tc = get(cax,'xcolor');
  53.  
  54. % only do grids if hold is off
  55. if ~hold_state
  56.  
  57. % make a radial grid
  58.     hold on;
  59.     hhh=plot([0 max(theta(:))],[0 max(abs(rho(:)))]);
  60.     v = [get(cax,'xlim') get(cax,'ylim')];
  61.     ticks = length(get(cax,'ytick'));
  62.     delete(hhh);
  63. % check radial limits and ticks
  64.     rmin = 0; rmax = v(4); rticks = ticks-1;
  65.     if rticks > 5    % see if we can reduce the number
  66.         if rem(rticks,2) == 0
  67.             rticks = rticks/2;
  68.         elseif rem(rticks,3) == 0
  69.             rticks = rticks/3;
  70.         end
  71.     end
  72.  
  73. % define a circle
  74.     th = 0:pi/50:2*pi;
  75.     xunit = cos(th);
  76.     yunit = sin(th);
  77.  
  78.     rinc = (rmax-rmin)/rticks;
  79.     for i=(rmin+rinc):rinc:rmax
  80.         plot(xunit*i,yunit*i,'-','color',tc,'linewidth',1);
  81.         text(0,i+rinc/20,['  ' num2str(i)],'verticalalignment','bottom');
  82.     end
  83.  
  84. % plot spokes
  85.     th = (1:6)*2*pi/12;
  86.     cst = cos(th); snt = sin(th);
  87.     cs = [-cst; cst];
  88.     sn = [-snt; snt];
  89.     plot(rmax*cs,rmax*sn,'-','color',tc,'linewidth',1);
  90.  
  91. % annotate spokes in degrees
  92.     rt = 1.1*rmax;
  93.     for i = 1:max(size(th))
  94.         text(rt*cst(i),rt*snt(i),int2str(i*30),'horizontalalignment','center');
  95.         if i == max(size(th))
  96.             loc = int2str(0);
  97.         else
  98.             loc = int2str(180+i*30);
  99.         end
  100.         text(-rt*cst(i),-rt*snt(i),loc,'horizontalalignment','center');
  101.     end
  102.  
  103. % set viewto 2-D
  104.     view(0,90);
  105. % set axis limits
  106.     axis(rmax*[-1 1 -1.1 1.1]);
  107. end
  108.  
  109. % transform data to Cartesian coordinates.
  110. xx = rho.*cos(theta);
  111. yy = rho.*sin(theta);
  112.  
  113. % plot data on top of grid
  114. if strcmp(line_style,'auto')
  115.     q = plot(xx,yy);
  116. else
  117.     q = plot(xx,yy,line_style);
  118. end
  119. if nargout > 0
  120.     hpol = q;
  121. end
  122. if ~hold_state
  123.     axis('equal');axis('off');
  124. end
  125.  
  126. % reset hold state
  127. if ~hold_state, set(cax,'NextPlot',next); end
  128.  
  129.