programmatically setting "Confirm before exit MATLAB"
26 views (last 30 days)
Show older comments
I like to set the preference "Confirm before exit MATLAB" that can be set through preferences->general->confirmation dialog->confirm before exit matlab through script. I have following code for test,
function TestExit(action)
if ~nargin
action = 'init';
end
switch action
case 'init'
% Initialize the GUI
h0 = figure('Visible','off','WindowStyle','normal');
% Add close event callback
%set(h0, 'CloseRequestFcn', ...
% 'TestExit(''close'');');
% Enable "Confirm before exit MATLAB"
setpref('MATLAB', 'ConfirmExit', true);
% Check the current value
getpref('MATLAB', 'ConfirmExit')
case 'close'
ans = questdlg('Are you sure you want to quit?', ...
'Exit', 'Yes', 'No', 'Yes');
if strcmp(ans, 'No')
return;
end
% Close and exit the program
delete(gcf);
quit
end % End switch
end
I tested with multiple versions of MATLAB (R2020a through R2025b), it all failed to popup the confirmation dialog and exit MATLAB. I can manually start matlab, turn on the preference through GUI, and it works. my question is, is there a way to do it using scripts.
Thanks,
weiliang
4 Comments
dpb
about 1 hour ago
Is this Qt starting/controlling a MATLAB app, I gather? If that's the case, then you can add a callback function in in on the 'UIFigureCloseRequest' that traps the user action to close the window by whatever means. The default appdesigner code template for the callback function is
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
delete(app)
end
Insert the user dialog call therein before the delete(app) statement with any other cleanup wanted/needed and you won't have to mess around with the user preferences nor mucking with the install path.
Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!