title: Match legend to axes objects
body: <h2>Context:</h2>
  
  <p>I have a (programmatic) GUI which contains several <code>axes</code> objects inside some <code>uipanel</code> parenting structure. Some of these <code>axes</code> have <code>legend</code> objects associated, some don't.<br>
  I want to include a button in my GUI which copies the currently visible plot into a new figure including its <code>legend</code> if it has one.</p>
  
  <p>I know how to get the handles to the currently visible <code>uipanel</code> and all <code>axes</code> objects inside it. I also know how to tell the <code>axes</code> apart from the <code>legend</code>s.</p>
  
  <h2>Question:</h2>
  
  <p>How can I <strong>match</strong> the <code>legend</code>s to the <code>axes</code>?</p>
  
  <p>For example, in one case my GUI shows 2 <code>axes</code> with some plots, each of which has its own <code>legend</code>. When I click the 'export' button, I want 2 new figures to be created, each containing one <code>axes</code> with its corresponding <code>legend</code>.<br>
  What I'm currently able to do is</p>
  
  <ol>
  <li>put <em>everything in one figure</em> (they overlap in that case because their positions in the original <code>uipanel</code>s are the same),</li>
  <li>put <em>each axes and each legend</em> into their own respective figures,</li>
  <li>put <em>all axes in one and all legends in another</em> figure and</li>
  <li>put <em>all axes in their own figure</em> with <em>all legends</em> within the same panel.</li>
  <li>split it up <em>by panel</em>, that is, put <em>all subplots into the same figure</em> and each group of plots in their own figure.</li>
  </ol>
  
  <h2>Problem:</h2>
  
  <p>The problem is, I don't have the handles to either of those objects. I only have the handles to the <code>uipanel</code> objects. The graphics inside the panels are built by another function which contains all sorts of tricky stuff, but doesn't return handles. Also the parenting structure of said panels makes it rather hard to do this with tricks like <code>get(handles.panels{1},'Children')</code> because it will work in some, but not all cases.<br>
  I thought about simply exporting the panels (and have actually a working version which does this), but this has several problems, mainly related to figure tools and resizing. I want to get rid of the panels when I use the "Export" button.</p>
  
  <h2>Code Snippet / Example:</h2>
  
  <p>The following code snippet will create an example GUI with access to all handles I have access to in my complete GUI. Clicking the buttons will show the different versions I got to "work". What I want is one figure for each axes <em>including its legend</em>, if it has one. the 4th version (same parent) comes close, but breaks if it encounters <code>subplot</code>s, the 5th version (by panel) simply puts entire subplot groups into one window (in which case, at least, they don't overlap). Copy the code into a new <code>.m</code>file to try it.</p>
  
  <pre><code>function test
      figure(1)
      clf
      t=(0:0.1:10)'; %'// dummy comment
      p2 = uipanel('Visible','off','Position',[0 0 1 1]);
      p1 = uipanel('position',[0 0 1 1]);
      p11 = uipanel('Parent',p1,'Position',[0 0 0.5 0.9]);
      p12 = uipanel('Parent',p1,'Position',[0.5 0 0.5 0.9]);
      uicontrol('Style','push','String','all in one','Units','norm',...
          'Position',[0.05 0.91 0.14 0.06],'Callback',@export1);
      uicontrol('Style','push','String','all in own','Units','norm',...
          'Position',[0.24 0.91 0.14 0.06],'Callback',@export2);
      uicontrol('Style','push','String','by type','Units','norm',...
          'Position',[0.43 0.91 0.14 0.06],'Callback',@export3);
      uicontrol('Style','push','String','same parent','Units','norm',...
          'Position',[0.62 0.91 0.14 0.06],'Callback',@export4);
      uicontrol('Style','push','String','same panel','Units','norm',...
          'Position',[0.81 0.91 0.14 0.06],'Callback',@export5);
      subplot(1,1,1,'Parent',p11)
      plot(t,[sin(t) cos(t)])
      legend('Sine','Cosine')
      subplot(2,1,1,'Parent',p12)
      plot(t,[polyval([0.05 -1 2],t) exp(-t) abs(t-3)])
      subplot(2,1,2,'Parent',p12)
      plot(t,erf(t))
      legend('Error function')
  
      function export1(~,~)
          current = findobj('Type','uipanel','Parent',1,'Visible','on');
          visible_axes = findobj(current,'Type','axes');
          copyobj(visible_axes,figure);
      end
  
      function export2(~,~)
          current = findobj('Type','uipanel','Parent',1,'Visible','on');
          visible_axes = findobj(current,'Type','axes');
          for i=1:length(visible_axes)
              copyobj(visible_axes(i),figure);
          end
      end
  
      function export3(~,~)
          current = findobj('Type','uipanel','Parent',1,'Visible','on');
          visible_axes = findobj(current,'Type','axes','Tag','');
          visible_legends = findobj(current,'Tag','legend');
          copyobj(visible_axes,figure);
          copyobj(visible_legends,figure);
      end
  
      function export4(~,~)
          current = findobj('Type','uipanel','Parent',1,'Visible','on');
          visible_axes = findobj(current,'Type','axes','Tag','');
          visible_legends = findobj(current,'Tag','legend');
          for i=1:length(visible_axes)
              par = get(visible_axes(i),'Parent');
              same = findobj(visible_legends,'Parent',par);
              h=figure;
              copyobj(visible_axes(i),h)
              copyobj(same,h)
          end
      end
  
      function export5(~,~)
          current = findobj('Type','uipanel','Parent',1,'Visible','on');
          visible_axes = findobj(current,'Type','axes');
          parents = cell2mat(get(visible_axes,'Parent'));
          uparents = unique(parents);
          for i=1:length(uparents)
              copyobj(visible_axes(parents==uparents(i)),figure)
          end
      end
  end
  </code></pre>