Matching Legend to Axis Objects

Context:

I have a (software) GUI that contains several axes objects inside some uipanel parent style uipanel . Some of these axes are associated with legend objects, and some are not.
I want to include a button in my graphical interface, which copies the current visible graph to a new figure, including its legend , if any.

I know how to get handles for currently visible uipanel objects and all axes inside it. I also know how to say axes other than legend s.

Question:

How can I map legend to axes ?

For example, in one case, my graphical interface shows 2 axes with some graphs, each of which has its own legend . When I click the Export button, I want to create two new digits, each of which contains one axes with the corresponding legend .
What I can do now is

  • place everything on one shape (they overlap in this case, because their positions in the original uipanel match),
  • put each axis and each legend in their respective numbers,
  • put all the axes in one and all the legends in another drawing and
  • place all the axes in your figure with all the legends within the same panel.
  • divide it into a panel i.e. put all the subheadings in one shape and each group of charts on their own shape.

Problem:

The problem is that I do not have descriptors for any of these objects. I only have handles for uipanel objects. The graphics inside the panels are built by another function that contains all kinds of complex things, but does not return descriptors. In addition, the structure of the parent panels of these panels makes this difficult with get(handles.panels{1},'Children') type tricks get(handles.panels{1},'Children') , since it will work in some, but not all cases.
I thought about just exporting the panels (and actually this is the working version that does this), but this has a few problems, mainly related to the shape and resize tools. I want to get rid of the panels when I use the Export button.

Code snippet / example:

The following code snippet will create an example GUI with access to all the hands that I have access to in my full graphical interface. By clicking on the buttons, you will see different versions that I received in order to "work". I want one shape for each axis, including its legend, if any. The 4th version (the same parent) approaches, but breaks if subplot s is encountered, the 5th version (on the panel) simply puts entire groups of subtasks in one window (in this case, at least, they do not overlap). Copy the code into the new .m file to try it.

 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 
+7
matlab legend axes
source share
3 answers

In the figure, graphic objects are organized hierarchically and can be processed individually. For example, axes are a child of a figure, a plot is a child of axes, and legends are constructed as axes.

The following example displays 2 lines (red and blue with legends), then mixes graphics and legends using copyobj .

 figure; subplot(1,2,1) hp1 = plot(1:10,'r') hl1 = legend('red') subplot(1,2,2) hp2 = plot(1:10,'b') hl2 = legend('blue') hf = figure; hax = axes; copyobj(hp1, hax); %copy plot to axes copyobj(hl2, hf); %copy legend to figure 

enter image description here

enter image description here

Not tested with GUI.

+2
source share

I think a simpler solution is to save the axes of the shape that you are going to save as a fig file.

 h = figure(1); x = linspace(1,100); y = 2*x; ax = findall(h,'type','axes'); plot(x,y); save('youraxes', 'ax'); hgsave(h, 'yourfig.fig'); 

I use Matlab R2012a, otherwise in R2013a or b the save image function is now savefig .

+1
source share

After you get the axle handle, you can find the corresponding legend handle using

legend_handle = legend(axes_handle)

0
source share

All Articles