Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell legends from axes in Matlab?

The following stackoverflow qestion:

Matlab: How to obtain all the axes handles in a figure handle?

identifies how to get handles to all of the axes from a figure in Matlab. However, this list will also contain handles to legends, at least in R2008a, which appear to also be axes. How can I tell (programatically) the legends from the real plot axes in a vector of axes handles?

like image 645
crobar Avatar asked Jan 30 '12 22:01

crobar


People also ask

How do I find the legend in MATLAB?

legend(___,'Location', lcn ) sets the legend location. For example, 'Location','northeast' positions the legend in the upper right corner of the axes. Specify the location after other input arguments. legend(___,'Orientation', ornt ) , where ornt is 'horizontal' , displays the legend items side-by-side.

What is a legend in MATLAB?

legend places a legend on various types of graphs (line plots, bar graphs, pie charts, etc.). For each line plotted, the legend shows a sample of the line type, marker symbol, and color beside the text label you specify.

How do you find the axes of a figure in MATLAB?

ax = gca returns the current axes (or standalone visualization) in the current figure. Use ax to get and set properties of the current axes. If there are no axes or charts in the current figure, then gca creates a Cartesian axes object.

How do I change the legend in MATLAB?

To add a legend title, set the String property of the legend text object. To change the title appearance, such as the font style or color, set legend text properties. For a list, see Text Properties. plot(rand(3)); lgd = legend('line 1','line 2','line 3'); lgd.


2 Answers

From linkaxes, the code you want is:

ax = findobj(gcf,'type','axes','-not','Tag','legend','-not','Tag','Colorbar');

This will return the handles of all the data axes in the current figure.

like image 165
Nzbuu Avatar answered Oct 15 '22 07:10

Nzbuu


1) By default the Tag property of legend is 'Legend'. Of course, there is no promise that it is not changed.

 get(l)

 ....
 BusyAction: 'queue'
      HandleVisibility: 'on'
               HitTest: 'on'
         Interruptible: 'off'
              Selected: 'off'
    SelectionHighlight: 'on'
                   **Tag: 'legend'**
                  Type: 'axes'
         UIContextMenu: 200.0018
              UserData: [1x1 struct]

 ....

2) Another difference (which is more robust) is that regular axes do not have String property, but legends do. I am not sure whether there are other types of objects that also have String property. For example:

  plot(magic(3));legend('a','v','b');
  allAxesInFigure = findall(f,'type','axes')
  b = isprop(allAxesInFigure,'String')

You can verify it by calling:

get(gca,'String')
??? Error using ==> get
There is no 'String' property in the 'axes' class.

But on the other hand, for legends there is such a property. That is why it is more robust.

 plot(magic(3)); l = legend('a','b','c');
 get(l,'String')

ans = 'a' 'b' 'c'

3) I would recommend to solve this in a broader context. Just keep track of the legends and axes you create by storing their handles. That is, instead of coding like:

 plot(magic(3));
 legend('a','v','b');
 plot(magic(5));
 legend('a','v','b','c','d');

Code like this:

 p(1) = plot(magic(3));
 l(1) = legend('a','v','b');
 p(2) = plot(magic(5));
 l(2) = legend('a','v','b','c','d');
like image 45
Andrey Rubshtein Avatar answered Oct 15 '22 08:10

Andrey Rubshtein