title: determining the bounding box around the contours
body: <p>I am trying to determine the bounding box around a specific contour level in the contourf plot:</p>
  
  <ol>
  <li>Plot the contourf plot of the data</li>
  <li>Plot the contour with level 0.075 on the same plot.</li>
  <li>Extract the contour data from the contour level</li>
  <li>Calculate the centroid for each contour data</li>
  <li>Do loop for each element of centroid x y and calculate distance to other 
  centroid x y. Merge Contour data with distances less than threshold of 5, 
  and place it in areas.</li>
  <li>Replot data and create boxes surrounding area x y data for each area on the 
  same plot.</li>
  </ol>
  
  <p>Here is the code which generates the contourf plot and tries to create bounding box:</p>
  
  <pre><code>axes(handles.axes1);
  cla(handles.axes1);
  
  contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
  colormap(handles.axes1,'jet');
  colorbar(handles.axes1);
  axis equal
  caxis([cMin cMax]);
  hold on
  
  
  [Cm,hm] = contour(xC(1,:),yC(:,1),zDiff,[0.075 0.075],'k');
  axis equal
  
  
  Sm = contourdata(Cm);
  
  hwb = waitbar(0.1, 'Processing')
  
  [p,q] = size(Sm);
  
  
  for i = 1:q
  
      pts = centroid(Sm(1,i).xdata,Sm(1,i).ydata);
      format long
      centCt(i,1) = pts(1);
      centCt(i,2) = pts(2);
  end
  
  
  [centCxs,indxs] = sort(centCt(:,1));
  centCys = centCt(indxs,2);
  
  
  Se = Sm;
  
  for i = 1:q
  
  Se(1,i).flag = 0;
  
  
  
  end
  
  
  ct = 1;
  
  for j = 1:q
  datax = [];
  datay = [];
   ct2 = 1;
      for i = 1:q
  
          rcentd  = sqrt((centCxs(i,1) - centCxs(j,1)).^2 + (centCys(i,1) - centCys(j,1)).^2);
  
          if(rcentd &lt; 5)
              if(Se(1,i).flag == 0)
                datax = [datax;Se(1,i).xdata];
                datay = [datay;Se(1,i).ydata];
  
                Se(1,i).flag = 1;
  
  
                ct2 = ct2 + 1;
  
              end
  
          end
      end
  
  if(ct2 &gt; 5)
  
       area(ct).xdat = datax;
       area(ct).ydat = datay;
       ct = ct + 1;
  end
  
  
  end
  
  
  close(hwb);
  
  
  
  cla(handles.axes1);    
  contourf(xC(1,:),yC(:,1),zDiff, 200,'LineColor','none');
  colormap(handles.axes1,'jet');
  colorbar(handles.axes1);
  axis equal
  caxis([cMin cMax]);
  hold on
  
  hold(handles.axes1,'on');
  
  
  for i = 1:length(area)
      [rectx,recty] = minboundrect(area(i).xdat,area(i).ydat);
      plot(rectx,recty,'-k');   
  end
  </code></pre>
  
  <p>Initial Contour Picture:</p>
  
  <p><a href="https://i.stack.imgur.com/hmjlo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hmjlo.png" alt="enter image description here"></a></p>
  
  <p>Results I am getting:</p>
  
  <p><a href="https://i.stack.imgur.com/UP3RE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UP3RE.png" alt="enter image description here"></a></p>
  
  <p>Desired Result:</p>
  
  <p><a href="https://i.stack.imgur.com/KUMc0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KUMc0.png" alt="enter image description here"></a></p>