Seeking a fast way to determine if axes are current

I've written a function that includes a line to determine if axes are current. This is the line:
AxesAreCurrent = ~isempty(findobj(gcf,'type','axes'));
From the profiler I see that about three quarters of the processing time of my 400 line function is taken up by this line. It's only a fraction of a second, but I will be calling this function thousands of times in a row, and I'd like to speed it up. Is there a faster check to determine if any axes are current?

댓글 수: 5

José-Luis
José-Luis 2014년 9월 29일
편집: José-Luis 2014년 9월 29일
What do you mean by "if axes are current"? What the code you posted does, unless I am mistaken, is finding out whether there are any axes in the current figure. I interpret your question as how to find out if some handle is equal to the current axes:
some_handle == gca
If you explicitly keep track of the objects you generate then the logical test might become unnecessary, or at least much faster.
How about using:
AxesCurrent = ~isempty(get(gcf,'children'));
or if there are any other uicontrols available then you can additionally use
get(get(gcf,'children'),'type')
and then see if there are any of type axes. but that might be just as long if not longer than findobj.
Also what is your exact desire? here all this would check if they exist and not if they've been updated for each iteration
Hi Joseph and José-Luis. My function plots some surfaces. But before plotting these surfaces, I'd like to determine if a plot is already open. If a plot is not already open, my function plots the surfaces then sets lighting, sets axis limits, sets view angle, etc. On the other hand, if when the function is called, a figure is already open and axes are current, my function adds the new surfaces to the plot without attempting to set lighting or axis limits.
The intent is to let the function figure out, without user input, if initial formatting needs to be applied.
Tricky...especially since gca will create axes if they don't exist.
exactly.

댓글을 달려면 로그인하십시오.

 채택된 답변

José-Luis
José-Luis 2014년 9월 29일
편집: José-Luis 2014년 9월 29일
isempty(get(gcf,'CurrentAxes'))
will tell you if there are any current axes in gcf . This is basically an edit of the gca() function that does not create new axes if none exist.

댓글 수: 2

Nice! Takes about 1/3 of the time of my original findobj method. Thanks José-Luis.
My pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Alexander Laut
Alexander Laut 2017년 9월 14일
i don't know how this compares but i found this solution useful for my needs to check if an axis exists without creating a figure so i can reset the color order
%%ResSet Color Order
if ~isempty(findobj(allchild(0),'Type','axes'))
set(gca(),'ColorOrderIndex',1) % resets color order if an axis exists
end

댓글 수: 1

There are a couple of issues with this piece of code. I assume you're trying to find even figures with hidden figure handles, which is why you used allchild. However, if the axes also has a hidden figure handle, then findobj won't find it, though findall would. If the axes handle is visible, but the figure handle is not, then gca will create a new figure and axes. For example:
f = figure('HandleVisibility','off');
a = axes('HandleVisibility','on','Parent',f);
if ~isempty(findobj(allchild(groot),'Type','axes'))
set(gca,'ColorOrderIndex',1)
end
If you want to deal with hidden handles, I'd suggest something more akin to this:
hAxes = findall(groot,'Type','axes')
if ~isempty(hAxes)
set(hAxes(1),'ColorOrderIndex',1)
% Or even set(hAxes, ...) to change all properties of all axes
end
It's not as clean, since there's no guarantee that hAxes(1) is the most recently clicked axes. However, a hidden handle figure/axes will never be in the 'CurrentFigure' or 'CurrentAxes' property, so that is kind of moot. I'll post something that can handle this in a separate answer.
-Cam

댓글을 달려면 로그인하십시오.

Cam Salzberger
Cam Salzberger 2017년 9월 14일
편집: Cam Salzberger 2017년 9월 14일
As Alexander pointed out, it may be desirable to avoid creating a figure with gcf. However, gca will have the same issues, if there are hidden handles. Here's something that can handle all hidden handle situations:
I'd suggest first checking for visible figures and axes without creating them, before then checking for hidden ones:
hFig = get(groot,'CurrentFigure');
if ~isempty(hFig)
hAx = get(hFig,'CurrentAxes');
if isempty(hAx)
hAxes = findall(hFig,'Type','axes');
hAx = hAxes(1);
end
else
hFigures = findall(groot,'Type','figure');
hAx = gobjects(0,0);
if ~isempty(hFigures)
k = 1;
while k < numel(hFigures) && isempty(hAx)
hAxes = findall(hFigures(k),'Type','axes');
if ~isempty(hAxes)
hAx = hAxes(1);
end
k = k+1;
end
end
end
You know, just to be really thorough. Now hFig and hAx will be a figure or axes handle respectively, or empty if there isn't one.
Qualifier: This is largely untested code, but hopefully can give you a guideline to work from.
-Cam

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2014년 9월 29일

편집:

2017년 9월 14일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by