131 views (last 30 days)

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?

Bjorn Gustavsson
on 25 Aug 2016

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

Erik
on 1 Oct 2016

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

PepijnB
on 11 Jul 2018

Edited: 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 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

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## 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

⋮## 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

## 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_387234

⋮## 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_387234

## 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_387237

⋮## 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_387237

## 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

⋮## 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.