Distinguish uifigure from figure programmatically

16 ビュー (過去 30 日間)
Adam
Adam 2017 年 7 月 12 日
コメント済み: Matt J 2024 年 1 月 28 日
Is it possible to distinguish a uifigure (appdesigner) from an old-style figure programmatically by an if type test?
class( hFig )
returns
'matlab.ui.Figure'
for both of them and I can't see anything obvious in the metaclass or properties that I can use to check which it is.
I could just use a try-catch, but I'd prefer to just filter uifigures than catch the error (I have a BusyCursor class which changes the pointer on all open figures to the busy cursor and then back again when it is deleted, but it crashes on uifigures which do not support changes to the 'pointer' property).
  2 件のコメント
David Young
David Young 2021 年 5 月 21 日
The solutions so far are all complex and sometimes undocumented. It seems strange that even in R2021a there isn't a simple isuifigure (or equivalent) function as part of the system. I can't believe users are expected to delve into the error generation mechanism of uialert to find out what to do. Using try-catch seems the simplest and most reliable way - and that's just horrible.
Adam Danz
Adam Danz 2021 年 5 月 21 日
It depends on what you're doing with the figure. Try/catch won't do squat if you need to work with the figure position property, for example, since the two types of figures have different position definitions and getting/setting position won't cause an error.

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

採用された回答

Ralph Coleman
Ralph Coleman 2019 年 9 月 18 日
編集済み: Ralph Coleman 2019 年 9 月 18 日
Hi
From R2018b, you can simply use:
matlab.ui.internal.isUIFigure(hFig)
  5 件のコメント
Markus Leuthold
Markus Leuthold 2022 年 6 月 14 日
looking at the code of isUIFigure, it boils down to checking the property JavaFrame_I
% if JavaFrame is empty, then this is a web figure.
if ~isempty(f) && isempty(get(f,'JavaFrame_I'))
bool = true;
else
bool = false;
end
Matt J
Matt J 2024 年 1 月 28 日
however regular figures generated in live editor are incorrectly identified as UIFigures using Ralf's method
I wonder if that's really incorrect. You could argue that a Live Scipt is an app window environment, so figures embedded there should be seen as uifigures.

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

その他の回答 (4 件)

Zhengyi
Zhengyi 2019 年 2 月 18 日
編集済み: Zhengyi 2019 年 2 月 18 日
TL;DR
function val = isuifigure(h)
val = ~isempty(matlab.ui.internal.dialog.DialogHelper.getFigureID(h));
end
The slightly longer story
If you type the following into the console, you will get an error:
>> uialert(figure,'a','b')
Error using uialert (line 42)
First argument must be a figure handle created using the uifigure function.
On line 42 of uialert function, we can see it uses the following method to validate the input handle:
matlab.ui.internal.dialog.DialogHelper.validateUIfigure(hUIFigure)
By reading through this function, we can see the error is thrown when the following function returns empty:
matlab.ui.internal.dialog.DialogHelper.getFigureID(h)
and
function out = getFigureID(f)
out = f.getId();
end
However,
getId()
seems to be a private function. The only option we have is to use getFigureID and check whether it returns a non-empty value.
Finaly, if you like, you can create this utility function to help you get over the long function call:
function val = isuifigure(h)
val = ~isempty(matlab.ui.internal.dialog.DialogHelper.getFigureID(h));
end
  1 件のコメント
Adam Danz
Adam Danz 2021 年 3 月 4 日
編集済み: Adam Danz 2021 年 3 月 7 日
Thanks for digging, @Zhengyi. This solution works all the way back to r2016a which is when uifigures were released. However, is breaks in r2020b (and, perhaps, prior releases) when the handle was created by figure().
matlab.ui.internal.dialog.DialogHelper.getFigureID(figure)
% Error:
% First argument must be a figure handle created using the uifigure function.
A workaround would be to use a try/catch and to test the MException for the error ID 'MATLAB:uitools:uidialogs:NotAnAppWindowHandle' but, according to your answer, that line returns an empty value in earlier releases and who knows if this error message will change in the future.

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


Dev-iL
Dev-iL 2017 年 8 月 15 日
I tried comparing the outputs of:
F = struct(uifigure());
G = struct(figure());
There are several fields that are different by default. Of increased interest are:
JavaFrame, Controller, ControllerInfo, NodeChildren
I cannot guarantee that it will work 100% of the time, but you can try testing for
isstruct(struct(hFig).ControllerInfo)
... if the above returns "true" - it's likely a uifigure.

Andreas Klotzek
Andreas Klotzek 2019 年 1 月 9 日
Hi
I also need an answer to this question.
The answers provided here are not certain to give the correct result. For example if I have a old-style figure with property HandleVisibility = 'off', the test using groot will give the wrong result.
Using properties is also not a stable idea. The behavior might change.
The problem is not only for figure and uifigure. I need the same check on axes/uiaxes or uipanel handles, basically on any graphics handle. One could probably use the ancestor function to redirect the problem for any graphics handle to the figure handle.

Celso Reyes
Celso Reyes 2018 年 8 月 20 日
編集済み: Celso Reyes 2018 年 8 月 21 日
The new figures do not show in groot by default -- At least as of Mac version, R2018a -- when ShowHiddenHandles is 'off'. If ShowHiddenHandles is 'on', then it will appear.
As a side note, the new-style uifigure appears when you use allchild(groot) regardless of the ShowHiddenHandles value.
So, one could non-invasive check would look like so...
function style = figtype(f)
g = groot;
oldStatus = g.ShowHiddenHandles;
g.ShowHiddenHandles = 'off';
if any(g.Children == f)
style = 'figure';
else
style = 'uifigure';
end
g.ShowHiddenHandles = oldStatus;
end
So, in practice...
>> newf = uifigure('Name','new');
>> oldf = figure('Name','old');
>> figtype(newf)
ans =
'uifigure'
>> figtype(oldf)
ans =
'figure'
I'm not sure what would make a regular figure's handle hidden, since the Visibility property doesn't do it.

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by