Asked by Erik
on 25 Aug 2016

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?

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

Guillaume
on 26 Aug 2016

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

Adam Danz
on 4 May 2019

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'.

per isakson
on 11 Jul 2018

isgraphics was introduced in R2014b

Adam Danz
on 4 May 2019

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')

Jordan Mandel
on 9 Apr 2019

This is a pretty remarkable find because it seems that this functionality is undocumented.

Adam
on 10 Apr 2019

It's documented in R2019a. Don't know about earlier versions.

Adam Danz
on 4 May 2019

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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Steven Lord (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/300880-what-is-best-practice-to-determine-if-input-is-a-figure-or-axes-handle#comment_387141

## Erik (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/300880-what-is-best-practice-to-determine-if-input-is-a-figure-or-axes-handle#comment_387234

## Adam (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/300880-what-is-best-practice-to-determine-if-input-is-a-figure-or-axes-handle#comment_387237

## adams13 (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/300880-what-is-best-practice-to-determine-if-input-is-a-figure-or-axes-handle#comment_421245

Sign in to comment.