Check if GUI is open

58 ビュー (過去 30 日間)
Pollok Tim
Pollok Tim 2016 年 9 月 21 日
回答済み: Kristoffer Walker 2020 年 10 月 21 日
Hi everybody
I believe the answer to this question already exists several times online, but everything I tried failed.
I got in a GUI-figure the option to open an other GUI-figure. In the second GUI I got plots and the first one can update these plots. Therefore its interesting to know if the second GUI is already/still open when I activate the update.
I tried:
~isempty(findobj('Tag','figure2'))
~isempty(findobj('Tag','axesInFigure2'))
~isempty(get(groot,'figure2'))
isgraphics(h,'figure2')
but I get the same result no matter if figure2 is open or not.
Is there an other way to get this information? I think of eventlisteners: figurehasbeenopened, figurehasbeenclosed
Thanks for help.

採用された回答

Adam
Adam 2016 年 9 月 21 日
編集済み: Adam 2016 年 9 月 21 日
Keep hold of the handle of the figure when you launch it.
Then just do a
if ishghandle( otherFigureHandle )
or
if isgraphics( otherFigureHandle )
test when you want to see if it is open or not.
  2 件のコメント
Pollok Tim
Pollok Tim 2016 年 9 月 21 日
Thanks a lot, now its running.
-Keep hold of the handle of the figure when you launch it.-
I understood that I get the handle of figure2 in its own openingFcn like this
% Get handle of figure2
figure2 = findobj('Type','figure','name','figure2');
% Saves figure2 for access in other .m-files
setappdata(0,'figure2',figure2);
And when I check if its open I use
% Get access to figure2
hfigure2 = getappdata(0,'figure2 ');
% either isgraphics() or ishghandle()
isgraphics(hfigure2);
ishghandle(hfigure2);
Both functions seem to work.
Adam
Adam 2016 年 9 月 21 日
It depends where you are launching it from. I never use findobj myself (well, rarely) because I keep hold of all my handles if I need them in the future as e.g.
hFig = MyGUI(...)
to launch a GUI named MyGUI.
Then later on I can just use hFig. I can't tell from your question whether you mean you have a custom-made figure (e.g. from GUIDE) or whether you mean just a standard launched figure.
I would not recommend using
setappdata( 0,... )
generally, especially with common names like 'figure2', but if it works then ok. I don't use setappdata most of the time, but if I do I attach my data to a specific figure handle rather than the root object to keep it linked to the specific application.

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

その他の回答 (4 件)

Benjamin Avants
Benjamin Avants 2017 年 10 月 17 日
I know this question is old, but GUIDE created figures don't show up as results from findobj.
Instead, use
figure2handle = findall(0,'Tag','figure2');
if ~isempty(figure2handle)
% figure exists
end

Vahab Youssofzadeh
Vahab Youssofzadeh 2017 年 6 月 2 日
編集済み: Vahab Youssofzadeh 2017 年 6 月 2 日
Here's another solution:
h = get(0, 'Children');
isempty(findobj(h,'name','NAME OF THE GUI'));
% isempty (findobj (h,'tag','NAME OF THE GUI'));

Nepenthes
Nepenthes 2018 年 7 月 17 日
This is a little late, but I chanced upon this solution when tackling a similar problem. H.isvalid or isvalid(H) should work. H can be a single handle, or a vector of handles. K>> H = figure; K>> G = plot(rand(5)); K>> delete(G(3:4)); K>> G.isvalid ans = 1 1 0 0 1 K>> H.isvalid ans = 1 K>> delete(H) K>> H.isvalid ans = 0

Kristoffer Walker
Kristoffer Walker 2020 年 10 月 21 日
None of the above seems to work for me when I am trying to detect if a GUI created from AppDesigner is open. Does anyone know how to do this with AppDesigner GUI's?
Best regards,
Kris

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by