programmatically setting "Confirm before exit MATLAB"
조회 수: 26 (최근 30일)
이전 댓글 표시
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
dpb
대략 1시간 전
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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!