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

  1. function [cout,hand] = contour3(arg1,arg2,arg3,arg4,arg5)
  2. %CONTOUR3 3-D contour plot.
  3. %    CONTOUR3(Z) plots the contour lines of Z in 3-D.  
  4. %    CONTOUR3(Z,N) plots N contour lines in 3-D.  The default is N=10.
  5. %    CONTOUR3(X,Y,Z) or CONTOUR3(X,Y,Z,N) uses the matrices X and Y
  6. %    to define the axis limits.
  7. %
  8. %    C = CONTOUR3(...) returns contour matrix C as described in CONTOURC.
  9. %    [C,H] = CONTOUR3(...) returns a column vector H of handles to LINE
  10. %    objects, one handle per line. 
  11. %
  12. %     See also CONTOURC, CONTOUR.
  13.  
  14. %    Clay M. Thompson 3-20-91
  15. %    Modified 1-17-92, LS.
  16. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  17.  
  18. % check up front for 3.5 parsing and exchange for 4.0 order
  19. if nargin >= 4
  20.         if  min(size(arg3)) == 1        % must be 3.5 syntax
  21.                 t = arg1;
  22.                 arg1 = arg3;
  23.                 arg3 = t;
  24.                 t = arg4;
  25.                 arg4 = arg2;
  26.                 arg2 = t;
  27.                 clear t
  28.         end
  29. end
  30.  
  31. % true 4.0 parsing, etc.
  32. if(nargin < 1)
  33.         error('Not enough input arguments.')
  34. else
  35.  
  36.         %%%
  37.         %%% let's trim off the last arg if it's a string
  38.         %%% (line_spec) for now.  we'll do something with it later.
  39.         %%%
  40.         line_spec = '';
  41.  
  42.         num_args = nargin;
  43.         last_arg = eval(['arg',num2str(num_args)]);
  44.         if(isstr(last_arg))
  45.                 line_spec = last_arg;
  46.                 num_args = num_args - 1;
  47.         end
  48.  
  49. % get color order for cycling of contour colors unless a linetype was passed in
  50.     if ~isempty(line_spec)
  51.         [j1,j2] = colstyle(line_spec);
  52.     else
  53.         j1 = '-';
  54.         j2 = [];
  55.     end
  56.         if isempty(j2) % no color spec was given
  57.                 colortab = get(newplot,'colororder');
  58.                 [mc,nc] = size(colortab);
  59.         end
  60.  
  61.         %%%
  62.         %%% check x and y to be sure they are vectors, if they were inputs
  63.         %%%
  64.         if num_args > 2
  65.                 % if arg1 and arg2 are not vectors, make them vectors
  66.                 if min(size(arg1)) > 1
  67.                         arg1 = arg1(1,:);
  68.                 end
  69.                 if min(size(arg2)) > 1
  70.                         arg2 = arg2(:,1);
  71.                 end
  72.     elseif num_args == 2
  73.         if max(size(arg2)) == 1 % might be N or a contour level
  74.             if ~(arg2 == fix(arg2) & arg2 > 0)
  75.                 arg2 = [arg2 arg2];
  76.             end
  77.         end
  78.         end
  79.  
  80.         %%%
  81.         %%% now let's assemble the list of args to pass to the internal
  82.         %%% contour command, which only returns the data vector, and no longer
  83.         %%% does any plotting:
  84.         %%%
  85.         the_passed_args = ['arg1'];
  86.         for i = 2:num_args
  87.                 the_passed_args = [the_passed_args, ', arg', num2str(i)];
  88.         end
  89.  
  90.         %%%
  91.         %%% do the call, get the c vector:
  92.         %%%
  93.         the_call = ['c = contourc(', the_passed_args, ');'];
  94.         eval(the_call);
  95.     if nargout > 0
  96.         cout = c;
  97.     end
  98.  
  99.         %%% get hold state
  100.     ax = newplot;
  101.     next = lower(get(ax,'NextPlot'));
  102.     isholdon = ishold;
  103.  
  104.         %%%
  105.         %%% now we have to do the plot.
  106.         %%%
  107.     csize = size(c);
  108.         limit = csize(2);
  109.         i = 1;
  110.  
  111.         h = [];
  112.         color_h = [];
  113.  
  114.         while(i < limit)
  115.                 z_level = c(1,i);
  116.                 npoints = c(2,i);
  117.         nexti = i+npoints+1;
  118.         while (nexti < limit)
  119.             if (z_level == c(1, nexti))
  120.                 npoints = npoints + c(2,nexti) + 1;
  121.                 c(1,nexti) = nan;
  122.                 c(2,nexti) = nan;
  123.             else
  124.                 break;
  125.             end
  126.             nexti = i+npoints+1;
  127.         end
  128.                 xdata = c(1,i+1:i+npoints);
  129.                 ydata = c(2,i+1:i+npoints);
  130.                 zdata = z_level * ones(1,npoints);
  131.                 cu = line('XData',xdata,'YData',ydata,'ZData',zdata);
  132.                 h = [h; cu(:)];
  133.                 color_h = [color_h ; z_level];
  134.                 i = nexti;
  135.         end
  136.         if ~isholdon
  137.         view(322.5,30);
  138.         end
  139.  
  140.         if isempty(j2)
  141.                 % set linecolors - all LEVEL lines should be same color
  142.                 % first find number of unique contour levels
  143.                 [zlev, ind] = sort(color_h);
  144.                 h = h(ind);     % handles are now sorted by level
  145.                 ncon = length(find(diff(zlev))) + 1;    % number of unique levels
  146.                 if ncon > mc    % more contour levels than colors, so cycle colors
  147.                         % build list of colors with cycling
  148.                         ncomp = round(ncon/mc); % number of complete cycles
  149.                         remains = ncon - ncomp*mc;
  150.                         one_cycle = (1:mc)';
  151.                         index = one_cycle(:,ones(1,ncomp));
  152.                         index = [index(:); (1:remains)'];
  153.                         colortab = colortab(index,:);
  154.                 end
  155.                 j = 1;
  156.                 zl = min(zlev);
  157.                 for i = 1:length(h)
  158.                         if zl < zlev(i)
  159.                                 j = j + 1;
  160.                                 zl = zlev(i);
  161.                         end
  162.                         set(h(i),'linestyle',j1,'color',colortab(j,:));
  163.                 end
  164.         else
  165.         if ~isempty(j1)
  166.                        for i = 1:length(h)
  167.                             status = set(h(i),'linestyle',j1);
  168.                    end
  169.         end
  170.         if ~isempty(j2)
  171.                        for i = 1:length(h)
  172.                             status = set(h(i),'color',j2);
  173.                    end
  174.         end
  175.         end
  176.  
  177. %%%
  178. %%% If command was of the form 'cout = contour(...)', let's return
  179. %%% the results of the contourc command:
  180. %%%
  181.         if(nargout > 1)
  182.                 hand = h;
  183.         end
  184. end
  185.  
  186.