MATLAB Answers

Erik
0

What is best practice to determine if input is a figure or axes handle?

Asked by Erik
on 25 Aug 2016
Latest activity Edited by Adam Danz
on 4 May 2019
I've been confused about MATLAB graphics since change to the new graphics system. I do not know how I can check for a variable being a handle to a figure or axes. Functions that come to mind are:
ishandle
isgraphics
ishghandle
isa(x,'matlab.ui.Figure') % x being the variable representing a Figure or Axes
isa(x,'matlab.graphics.axis.Axes') % wow, these really are cumbersome commands!
% etc.
What are best practices to ensure maximal compatibility with most MATLAB versions to detect what kind of input I'm dealing with? Why are there no
isfigure
or
isaxes
functions built-in?

  4 Comments

Show 1 older comment
ancestor is a nice function I didn't know about yet. Thanks. However, it's not exactly what I'm looking for.
I'm simply looking for the best way to determine if a variable is an (array of) axes or figure handle(s). isgraphics seems to detect valid graphics handles, but then what is the best way to determine the object being axes or figure(s)?
It seems tedious to check using either
isa(obj,'matlab.graphics.axis.Axes')
strcmp(obj.Type,'axes')
isa(obj,'matlab.ui.Figure')
strcmp(obj.Type,'figure')
Because there are multiple options, I asked my question: what's the best/most reliable/most compatible way to detect the object I'm dealing with? Should I look at the 'Type' property? Should I look at the class? Why aren't there isfigure or isaxes functions?
I use a test on the class in the new system, though usually I am doing it for validation so I use:
validateattributes( hAxes, { 'matlab.graphics.axis.Axes' }, { 'scalar' } )
which I wrapped up with another command in my own validateAxes( hAxes ) function. To return a boolean though I would just wrap the isa test in my own isAxes( hAxes ) function so that the ugly code is hidden under an intuitively named function.
I do also tend to get mixed up as to the difference between ishghandle and isgraphics for a more generic test though.
If you are talking about compatibility than I would use a method that works also in Octave:
function isAxes = IsAxes(ax)
try
isAxes = strcmp(get(ax, 'type'), 'axes');
catch
isAxes = false;
end
end

Sign in to comment.

3 Answers

Answer by Bjorn Gustavsson on 25 Aug 2016
 Accepted Answer

Simples, write those functions yourself, something along the lines:
function OK = isfigure(h)
if strcmp(get(h,'type'),'figure')
OK = 1;
else
OK = 0;
end
You could decorate your code to handle array input, do error checking and make the output proper Boolean output but the core part is to look at the type field of the graphics handle.
HTH

  2 Comments

Well, if you're going to write your own test function, at least make it so it also works with arrays of handles:
function tf = isfigure(h)
tf = strcmp(get(h, 'type'), 'figure');
end
Like the other two answers here, this method also doesn't distinguish between a figure handle of class 'matlab.ui.Figure' and a double precision integer that happens to have the same value as a figure number ( I provided examples below the other two answers ). This becomes an issue in the case of a function with a variable number of input arguments (varargin) where an input could be a figure handle or an integer that has the same value as a figure number.
To make that distinction, you could check the class of the variable as well.
tf = strcmp(get(h, 'type'), 'figure') & isa(h, 'matlab.ui.Figure');

Sign in to comment.


Answer by Erik
on 1 Oct 2016

Another option I found inside the undocumented axescheck function is the specification of a type to check for when using isgraphics. The following function does the job for figures.
function tf = isfigure(f)
tf = isgraphics(f,'figure')
I didn't know isgraphics accepted the type to check for. This also works for 'axes'.

  2 Comments

isgraphics was introduced in R2014b
Take note that isgraphics() doesn't distinguish between a figure handle of class 'matlab.ui.Figure' and a double precision integer that happens to have the same value as a figure number.
For example,
function produceFigure(figHandle)
if isgraphics(figHandle, 'figure')
h = figHandle;
else
h = figure();
end
end
fig = figure(1);
x = 1;
produceFigure(fig) %recognized as figure handle
produceFigure(x) %recognized as figure handle, too

Sign in to comment.


Answer by PepijnB
on 11 Jul 2018
Edited by PepijnB
on 11 Jul 2018

In the 'eyediagram'-function (build-in Matlab function), Mathworks checks the validity of a handle by the code:
ishghandle(h,'figure')
To check whether the handle is from an axes, use the following:
ishghandle(h, 'axes')

  3 Comments

This is a pretty remarkable find because it seems that this functionality is undocumented.
It's documented in R2019a. Don't know about earlier versions.
This is the same comment as in another answer here but it's worth mentioning.
Take note that ishghandle() doesn't distinguish between a figure handle of class 'matlab.ui.Figure' and a double precision integer that happens to have the same value as a figure number.
For example,
function produceFigure(figHandle)
if ishghandle(figHandle, 'figure')
h = figHandle;
else
h = figure();
end
end
fig = figure(1);
x = 1;
produceFigure(fig) %recognized as figure handle
produceFigure(x) %recognized as figure handle, too

Sign in to comment.