How to detect the close window event for a "bar" graph

5 views (last 30 days)
I tried to use the CloseRequestFcn to specify the close action for a bar graph but it does not work. It seems that it only works for figures.
How can I detect the user action of closing a bar graph window and change its behavior?
Thanks in advance for your help!

Answers (1)

Adam Danz
Adam Danz on 4 Sep 2020
Edited: Adam Danz on 4 Sep 2020
To invoke an action when the bars are deleted, use the DeleteFcn property of the bar object. Bars can be deleted using cla(), delete(h) where h is the bar handle, or when the figure closes.
The CloseRequestFcn is invoked when the figure closes which will also invoke the DeleteFcn if the bars still exist. See a demo in the comments below this answer.
  22 Comments
Adam Danz
Adam Danz on 9 Sep 2020
@Chao just for future reference, when you override the close request function, you must explicitly close the figure within the callback function if want it to close.
For example,
f = figure();
f.CloseRequestFcn = @closeFigureFcn;
function closeFigureFcn(h,~)
fprintf('\n\nGoodbye!\n\n')
delete(h) % <-- delete figure
end
Also, if you have the figure handle, you can delete the figure at any time using delete(f) even if its closeRequestFcn is otherwise blocking its closure.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!