MATLAB Answers

Determine if using HG2

13 ビュー (過去 30 日間)
Oliver Woodford
Oliver Woodford 2014 年 6 月 21 日
回答済み: Cris Luengo 2017 年 2 月 20 日
How can I determine if MATLAB or the current figure is using the new graphics pipeline, HG2? I need a function
tf = ishg2(figure_handle)
which should be backwards compatible, say to MATLAB 7.0, and also account for the fact that people can choose which rendering pipeline to use (HG1 or HG2) in newer MATLAB releases, so simply checking version number is not sufficient.

  3 件のコメント

Oliver Woodford
Oliver Woodford 2014 年 6 月 21 日
The
get(gcf, 'usehg2')
property described here appears not to work in R2014b prerelease.
Robert Berglin
Robert Berglin 2016 年 4 月 13 日
It's so simple you may not want to bother to create a function:
function result = ishg2(figure_handle)
if isnumeric(figure_handle)
result=false;
else
result=true;
end
end
Walter Roberson
Walter Roberson 2016 年 4 月 23 日
figure handles are permitted to be numeric in hg2.

サインインしてコメントする。

採用された回答

Oliver Woodford
Oliver Woodford 2014 年 6 月 26 日
I've ended up using the undocumented function, graphicsversion():
function tf = ishg2(fig)
try
tf = ~graphicsversion(fig, 'handlegraphics');
catch
tf = false;
end

  1 件のコメント

Oliver Woodford
Oliver Woodford 2015 年 6 月 16 日
graphicsversion is undocumented and may disappear in the future. Cris Luengo's first suggestion might be a better option.

サインインしてコメントする。

その他の回答 (5 件)

Jan
Jan 2015 年 7 月 31 日
編集済み: Jan 2016 年 2 月 29 日
A summary of the solutions posted here, which use a figure handle h:
exist('graphicsversion', 'builtin') && ~graphicsversion(h, 'handlegraphics')
isgraphics(h) && isa(h,'handle')
isa(h, 'handle')
~isnumeric(h)
isa(h, 'matlab.ui.Figure')
~graphicsversion(h, 'handlegraphics') % Not documented
get(h, 'usehg2') % Not working in some versions
A version without using a figure handle:
~verLessThan('matlab', '8.4')
[100, 1] * sscanf(version, '%d.', 2) < 804 % 170 times faster than verLessThan method
The function ishg2figure was part of Matlab2006a already, but it is private and not documented.
Many codes depend on the used handle graphics version. Therefore a reliable an efficient identification is required and the corresponding tool should not be determined by a discussion in the forum, but by an officially documented Matlablab function. In addition this function must be available for older Matlab versions also, such that I need a download e.g. in the FileExchange or the knowledge base.

  0 件のコメント

サインインしてコメントする。


Oliver Woodford
Oliver Woodford 2014 年 6 月 24 日
編集済み: Oliver Woodford 2014 年 6 月 24 日
Currently I'm using the version checking approach:
function tf = ishg2()
try
tf = ~verLessThan('matlab', '8.4');
catch
tf = false;
end

  0 件のコメント

サインインしてコメントする。


Cris Luengo
Cris Luengo 2014 年 10 月 27 日
You can simply check the class of the figure handle:
function tf = ishg2(fig)
tf = isa(h,'matlab.ui.Figure');
or:
function tf = ishg2(fig)
tf = ~isnumeric(fig);
In the second case you are not checking whether the input is a figure handle or not, buyer beware.

  0 件のコメント

サインインしてコメントする。


Markus Leuthold
Markus Leuthold 2014 年 11 月 11 日
For any handle returned by a plot command,
isa(h,'handle')
returns true if HG2 is used, false otherwise. The command does not check if h is still a valid handle. If you need that, then
isgraphics(h) && isa(h,'handle')
Works on both 2014b and older versions

  1 件のコメント

Mike Garrity
Mike Garrity 2014 年 11 月 11 日
That version's actually not safe. To help keep old code running in 14b, it's possible to cast a handle to a graphics object into a double.
The following returns false
ax = gca
h = double(ax)
if isgraphics(h) && isa(h,'handle')
disp('is')
else
disp('aint')
end
Similarly there were some tricky cases in earlier versions where you could cast the double value for a graphics object into a handle.
The graphicsversion function is actually the safest way to do this because it knows all of the different combinations.
If you need to run in an old version which didn't have the graphicsversion command, then you can assume that you don't have a new graphics object. So you could either use try/catch as Oliver showed, or do something like this:
exist('graphicsversion','builtin') && ~graphicsversion(h,'handlegraphics')

サインインしてコメントする。


Cris Luengo
Cris Luengo 2017 年 2 月 20 日
Revisiting this issue.
The best method discussed here is using `graphics` version. However, you currently get a warning when you use it, since it's deprecated. The help to that function suggests you compare the version number to 8.4:
verLessThan('matlab','8.4.0')
Jan Simon suggested this as a faster alternative:
[100, 1] * sscanf(version, '%d.', 2) < 804
However, since it's possible to turn off HG2 in at least some versions of MATLAB, this is not a fool-proof method.
An alternative could be:
try, hg2 = strcmp(get(fig,'GraphicsSmoothing'),'on'); catch, hg2 = false; end
The 'GraphicsSmoothing' is a property that only exists for HG2 figures.
The solution I had given earlier,
~isnumeric(fig)
only works if `fig` has not been converted to double. So you can use it on the result of `figure` or `gcf`, but if you write a function that takes a figure handle as input, and you want your uses to be able to simply type the figure number, then that method fails.

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by