What is best practice to determine if input is a figure or axes handle?
Ältere Kommentare anzeigen
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 Kommentare
Steven Lord
am 25 Aug. 2016
Can you say a little more about what you're doing where you need to distinguish the two? One use case I can think of where you need to distinguish is if you want to know the figure that contains the object whose handle you have (which would be itself if you have a figure handle.) In that case you could use the ancestor function.
f = figure;
ax = axes('Parent', f);
parentFigure1 = ancestor(f, 'figure');
parentFigure2 = ancestor(ax, 'figure');
If you try to ask for the axes ancestor of a figure, you'll receive an empty array.
parentAxes1 = ancestor(f, 'axes');
Erik
am 26 Aug. 2016
Adam
am 26 Aug. 2016
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.
adams13
am 18 Jan. 2017
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
Akzeptierte Antwort
Weitere Antworten (3)
Erik
am 1 Okt. 2016
2 Kommentare
per isakson
am 11 Jul. 2018
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 backward compatibility prior to r2014b).
close all
fig = figure(); %assigned fig 1
isfig = isgraphics(fig,'figure')
isfig = isgraphics(1,'figure')
This is because
handle(1)
To accept only figure handles and not matching integers from r2014b and later,
close all
fig = figure(); %assigned fig 1
isfig = isgraphics(fig,'figure') && isa(fig,'matlab.ui.Figure')
isfig = isgraphics(1,'figure') && isa(1,'matlab.ui.Figure')
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 Kommentare
Jordan Mandel
am 9 Apr. 2019
This is a pretty remarkable find because it seems that this functionality is undocumented.
Adam
am 10 Apr. 2019
It's documented in R2019a. Don't know about earlier versions.
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 backward compatibility prior to r2014b).
For example,
close all
fig = figure(); %assigned fig 1
isfig = ishghandle(fig,'figure')
isfig = ishghandle(1,'figure')
This is because
handle(1)
To accept only figure handles and not matching integers from r2014b and later,
isfig = ishghandle(fig,'figure') && isa(fig,'matlab.ui.Figure')
isfig = ishghandle(1,'figure') && isa(1,'matlab.ui.Figure')
I find myself revisiting this page from time to time and wanted to share an alternative best practice to determine if an array of inputs are figures or axes. It's based on Erik's solution using isgraphics(h,type) which is documented at least as far back as r2015b and functions in even older releases but with two differences:
- Distinguishes between figure or axes handles and double precision values that match handle values, after r2014a. For example, close all; h=figure; isgraphics(1,'figure') returns true which could be problematic in some contexts after r14a.
- Includes polaraxes for the is-axes test
Tested in r2020b & r2014a after removing features in the demo that didn't exist then.
% h is an array; function return a logical array the same size as h.
isfigure = @(h)isgraphics(h,'figure') & (verLessThan('matlab','8.4') | ~isa(h,'double'));
isaxes = @(h)(isgraphics(h,'axes') | isgraphics(h,'polaraxes')) & (verLessThan('matlab','8.4') | ~isa(h,'double'));
Demo:
% 5 figures and 5 axes
close all
vis = 'off';
h(1) = figure('Visible',vis); % Visibility not needed for the demo
h(2) = figure('Visible',vis);
h(3) = figure('Visible',vis);
h(4) = uifigure('Visible',vis);
h(5) = uifigure('Visible',vis);
h(6) = axes(h(1));
h(7) = polaraxes(h(2));
h(8) = subplot(2,2,1,'Parent',h(3));
h(9) = uiaxes(h(4));
tlo = tiledlayout(h(5),1,2);
h(10) = nexttile(tlo);
cleanup = onCleanup(@()delete(h));
isfig = isfigure(h)
% isfig =
% 1×10 logical array
% 1 1 1 1 1 0 0 0 0 0
isax = isaxes(h)
% isax =
% 1×10 logical array
% 0 0 0 0 0 1 1 1 1 1
This method correctly rejects double precision values that match the figure and axis handles when converted to double (>r14a):
val = double(h(1:3))
% val =
% 1 2 3
isfig = isfigure(val)
% isfig =
% 1×3 logical array
% 0 0 0
If you are intentionally using the double precision version of graphics handles after r2014a you can either remove those conditions from the functions or convert to handles in the inputs,
isfig = isfigure(handle(val))
% isfig =
% 1×3 logical array
% 1 1 1
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!