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

前端 未结 4 1384
甜味超标
甜味超标 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: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.
    

提交回复
热议问题