Cannot close figure using CloseFunctionRequest

13 ビュー (過去 30 日間)
Simon Aldworth
Simon Aldworth 2025 年 6 月 18 日
コメント済み: Adam Danz 2025 年 6 月 18 日
Hello,
I have created a gui using figure and a collection of uicontrols but I now find that I can't close the figure once I'm done with it. I can press the cross in the top right corner and the code runs satisfactorily but the figure won't disappear. Close(gcf) won't work but delete(gcf) will. The code was borrowed from the answer to this post: Answer, but here's my code stripped out to leave only the bits that I think are relevant:
function UserInput_short(S)
hfig = figure('CloseRequestFcn',@close_req_fun);
uiwait(hfig)
function close_req_fun(~,~)
uiresume
end
end
If I run the above I get a blank figure which I can't close using the cross at top right. What am I doing wrong?
Thanks.

採用された回答

Adam Danz
Adam Danz 2025 年 6 月 18 日
The CloseRequestFcn defines what to do when someone tries to close the figure. If you override the CloseRequestFcn then instead of simply closing, the code you wrote will run.
Option 1 is to include the delete command in the callback.
function close_req_fun(fig,~)
% [ define your custom behaviors ]
delete(fig)
end
Option 2 is to include a close button in your app that can be used to close the figure instead of the [x] menu button.
Option 3 is to remove the closeRequestFcn definition completely if you want the default closure behavior.
The reason the uiresume is in included in that answer is becuase the dialog also contains a uiwait so the [x] menu button can be used to resume execution and then delete the figure.
  1 件のコメント
Adam Danz
Adam Danz 2025 年 6 月 18 日
From the Answer you mentioned
  1. The figure waits here indefinitely
  2. By pressing [x] execution resumes
  3. The figure is deleted

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by