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

前端 未结 4 1383
甜味超标
甜味超标 2020-12-04 22:06

How do I obtain all the axes handles in a figure handle?

Given the figure handle hf, I found that get(hf, \'children\') may return the hand

相关标签:
4条回答
  • 2020-12-04 22:19

    "Jonas" and "tm1" have answers that work for some. However, as tm1 pointed the issue, there are several items inside type 'axes'.

    To exactly refer to the legend or axes itself (there may exist other items), you need to differentiate them, using their characteristic properties.

    In my example, I opened "property editor" and looked for differing properties for axes and legend (since they both belong to "type, axes"). I was trying to copy my axes and its legend:

    copied_axes = findobj(temp_fig,'type','axes','Tag','');
    copied_legend = findobj(temp_fig,'type','axes','Tag','legend');
    

    Instead of 'Tag' property, I also could use an other property from the "Property Inspector". The thing is, they must differ. Most of their properties are the same.

    0 讨论(0)
  • 2020-12-04 22:32

    Jonas' solution didn't work for me, because there were some handles referring to legends. Surprisingly, legends seem to be implemented as axes, at least in Matlab 2010a. Here is a solution if you only want the axes, not any legends or other stuff.

    axesHandles = get(fig, 'Children');
    classHandles = handle(axesHandles);
    count = length(axesHandles);
    isNotInstanceOfSubtype = false(1, count);
    for i = 1:count
        isNotInstanceOfSubtype(i) = strcmp(class(classHandles(i)), 'axes') == 1;
    end
    axesHandles = axesHandles(isNotInstanceOfSubtype);
    

    The script works by sorting out handles which reveal to be of a sub-type of type axes, for example scribe.legend.

    A warning for those trying to improve the above code snippet: using something like

    classHandles = cellfun(@(x) handle(x), axesHandles)
    

    might not work as intended:

    ??? Error using ==> cellfun
    scribe.legend type is not currently implemented.
    
    0 讨论(0)
  • 2020-12-04 22:35

    The solution by @tm1 is excellent. Mine is a little less complicated (if you're ok with functional programming):

    % initialize `fig` somehow, i.e., "fig=gcf()" for the current figure or
    % "fig=get(0,'children')" for all open figures; can be vector or scalar.
    
    ax = findall(fig, 'type', 'axes');
    ax = ax(arrayfun(@(i) strcmp(class(handle(i)), 'axes'), ax));
    

    ax will contain only plot axes. This works because the class of a legend or colorbar object is different from axes.

    Edit @Jonas points out a potential improvement to filter the results of findall, because at least legends and colorbars seem to have non-empty Tag properties: replace the last line in the above code snippet with

    ax = ax(strcmp('', get(ax, 'Tag')))
    

    Both these techniques are kludgy and may break in the future (a comparison against ggplot2 or Bokeh might be interesting).

    0 讨论(0)
  • 2020-12-04 22:46

    Use FINDALL:

    allAxesInFigure = findall(figureHandle,'type','axes');
    

    If you want to get all axes handles anywhere in Matlab, you could do the following:

    allAxes = findall(0,'type','axes');
    

    EDIT

    To answer the second part of your question: You can test for whether a list of handles are axes by getting the handles type property:

    isAxes = strcmp('axes',get(listOfHandles,'type'));
    

    isAxes will be true for every handle that is of type axes.

    EDIT2

    To select only axes handles that are not legends, you need to cleanup the list of axes (ax handles by removing all handles whose tag is not 'legend' or 'Colorbar':

    axNoLegendsOrColorbars= ax(~ismember(get(ax,'Tag'),{'legend','Colobar'}))
    
    0 讨论(0)
提交回复
热议问题