How to tell legends about axes in Matlab?

The following stackoverflow qestion example:

Matlab: How to get all the axes in a drawing pen?

Defines how to get the descriptors of all axes from a shape in Matlab. However, this list will also contain legend descriptors, at least in R2008a, which are also axes. How can I (programmatically) describe the legends from the real axes of the graph in the axis handle vector?

+6
source share
3 answers

From linkaxes , the code you want:

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

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

+12
source

1) By default, the Legend Tag property is Legend. Of course, there are no promises that he will not change.

  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 (more reliable) is that ordinary axes do not have the String property, but legends. I'm not sure if there are other objects that also have a String property. For instance:

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

You can verify this 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 reliable.

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

ans = 'a' 'b' 'c'

3) I would recommend solving this in a wider context. Just keep an eye on the legends and axes you create while keeping their pens. That is, instead of coding, for example:

  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'); 
+6
source

Just changing the code fooobar.com/questions/167893 / ... a bit:

 axesHandles = get(fig, 'Children'); classHandles = handle(axesHandles); count = length(axesHandles); isLegend = false(1, count); for i = 1:count isLegend(i) = strcmp(class(classHandles(i)), 'scribe.legend') == 1; end legendHandles = axesHandles(isLegend); 

Unfortunately, this decision depends on the implementation details.

0
source

Source: https://habr.com/ru/post/907268/


All Articles